views:

44

answers:

1

For a project I am working on, I am trying to do the following thing.

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.

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.

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.

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.

This can also be solved if one could explain my other question I have.

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.

  <Field ID="{7CC49D9D-F6F5-4A4A-851F-3152AAAAB158}" Type="Lookup"
     List="Intermediairs" Name="IntermediairLookup" DisplayName="Intermediair"
     StaticName="IntermediairLookup" Group="Onboarding" ShowField="Naam" />

You should know that this code seems to work:

        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 && 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();

        }

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.

Greetings,

Mats

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?

+1  A: 

Take a look at SPView.ViewFields

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spview.viewfields.aspx

Tom Vervoort
There couldn't have been a better answer. Haha, thanks a bunch! This does exactly what I want. I also found out how I can reorganize the view using that certain object, using MoveFieldTo. Awesome.
Mats Willemsen