Note that there are some explanatory texts on larger screens.

plurals
  1. POProgrammatically adding columns to Sharepoint List Views
    text
    copied!<p>For a project I am working on, I am trying to do the following thing.</p> <p>There's a Sharepoint 2010 environment, with a few Custom Lists created in Visual Studio. I am adding some fields to them using background code in the FeatureActivated function in the EventReceiver. </p> <p>What I am doing there is adding a lookup field to the Sharepoint List, and setting the properties to allow it to lookup values from the other list I am adding to the Sharepoint Site.</p> <p>However, I can't find a function to add it to one of the views. I've tried modifying the Schema Xml, but I can't seem to find a function to reinsert it to the List, and when using the Xml file from the View, I can't seem to make it work.</p> <p>Is there an easy way to programatically add a field to a view? This would help me out, since there seems to be no way to do this correctly.</p> <p>This can also be solved if one could explain my other question I have.</p> <p>I would like to know how one could make Lookup fields in the Schema XML file. I have a Custom Content Type, and Custom Fields, and I am currently trying to look up the Naam field in the Intermediairs List. (This one is also created when deploying this solution). When searching Google, it seems I have to use either a name / the GUID of a List Instance here, but I don't know the GUID of the List Instance beforehand.</p> <pre><code> &lt;Field ID="{7CC49D9D-F6F5-4A4A-851F-3152AAAAB158}" Type="Lookup" List="Intermediairs" Name="IntermediairLookup" DisplayName="Intermediair" StaticName="IntermediairLookup" Group="Onboarding" ShowField="Naam" /&gt; </code></pre> <p>You should know that this code seems to work:</p> <pre><code> SPWeb web = null; SPSite site = null; if (properties.Feature.Parent is SPWeb) { web = properties.Feature.Parent as SPWeb; site = web.Site; } if (properties.Feature.Parent is SPSite) { site = properties.Feature.Parent as SPSite; web = site.RootWeb; } web.AllowUnsafeUpdates = true; SPList changeList = web.Lists.TryGetList("Onboarding"); SPList sourceList = web.Lists.TryGetList("Intermediairs"); if (changeList != null &amp;&amp; sourceList != null) { changeList.Fields.Delete("IntermediairLookup"); var PrimaryColumnStr = changeList.Fields.AddLookup("Intermediair", sourceList.ID, true); var PrimaryColumn = changeList.Fields.GetFieldByInternalName(PrimaryColumnStr) as SPFieldLookup; PrimaryColumn.LookupField = sourceList.Fields["Naam"].InternalName; PrimaryColumn.Update(); } </code></pre> <p>But yeah. I can't figure out how to do it in XML form. Anyone has any ideas? A solution to either of the questions would solve my core issue.</p> <p>Greetings,</p> <p>Mats</p> <p>EDIT: Well, the question has now been answered, thanks again! One thing though. I would really like to know at some point how to do something like this in XML / CAML. Does anyone know how to do that? Anyone who's still reading this thread?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload