Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add a lookup field to a content type in sharepoint

How can I add a lookup field to a content type in sharepoint using the xml definition? (I'm getting errors).

Things to note: - The lookup list will exist when the content type is added to the document library. - The lookup list will always have the same name. - The lookup list has a space in the name.

This is what I've added to the xml:

  <Field ID="{GUID}"
         Type="Lookup"
         List="$Resources:core,lists_Folder;/List%20Name"
         ShowField="Title"
         Name="MyLookupFieldName"
         DisplayName="MyLookupFieldName"
         StaticName="MyLookupFieldName"
         Hidden="FALSE"
         Required="FALSE"
         Sealed="TRUE"
         >

When I then programatically add the content type to a document library I get an exception (with no useful information), and the following is logged to the sharepoint log:

08/18/2009 17:13:39.50 w3wp.exe (0x08B8) 0x11B0 Windows SharePoint Services Database 6f8g Unexpected Unexpected query execution failure, error code 8114. Additional error information from SQL Server is included below. "Error converting data type nvarchar to uniqueidentifier." Query text (if available): "{?=call proc_GetListMetaDataAndEventReceivers(?,?,?,?,?,?)}"

like image 596
Zarek Avatar asked Aug 18 '09 16:08

Zarek


3 Answers

Luckily, in SharePoint 2010, you can declaratively do this by setting all required properities as shown in the following working example.

<Field Type="Lookup" DisplayName="Link Type" Description="Represents link type." 
Required="TRUE" EnforceUniqueValues="FALSE" List="Lists/Links Types" WebId="~sitecollection" 
Overwrite="TRUE" PrependId="TRUE" ShowField="Title" UnlimitedLengthInDocumentLibrary="FALSE" 
Group="Research Links Columns" ID="{a15e9fa2-4ea0-41f1-a583-b21d53cf72d3}" 
SourceID="{30650f6f-fbb8-4acc-a935-29745f5d3c59}" StaticName="Link_x0020_Type" 
Name="Link_x0020_Type" Hidden="FALSE" ReadOnly="FALSE"></Field>

It's important to set WebId to have value of "~sitecollection" and set Overwrite to be TRUE.
More info

like image 167
Ahmed Atia Avatar answered Sep 18 '22 20:09

Ahmed Atia


The problem is that you need to reference the GUID of the list not its title. As you probably won't know the GUID of the list then you can't do this without executing some custom code afterwards.

Even if you aren't using VSeWSS, the last steps in the post dahlbyk has linked to show you how to do this. Chris O'Brien has gone to the trouble of making a CodePlex project that will help you if you aren't using VSeWSS.

like image 42
Alex Angas Avatar answered Sep 20 '22 20:09

Alex Angas


Ok, so I couldn't get the xml definition of a field for a content type to work for me for some reason. I did find out how to do it in code. The solution that worked for me is to not add the Field definition in xml, instead add it in code:

  • Add the content type to the list (in site definition code, or wherever).
  • Add a field lookup to the given SPWeb (so the field is a web? field, rather than a site field)
  • Add a new field link to the list content type.
  • Update the content type.

For example:

SPContentType myContentType = myWeb.Site.RootWeb.ContentTypes["MyContentType "];
myLib.ContentTypes.Add(myContentType);

myContentType = myLib.ContentTypes["MyContentType "];

myWeb.Fields.AddLookup("MyLookupFieldName", myWeb.Lists["MyLookupListName"].ID, false);
SPFieldLink myFIeldLink = new SPFieldLink(myWeb.Fields["MyLookupFieldName"]);
myContentType.FieldLinks.Add(myFIeldLink);
myContentType.Update();
like image 34
Zarek Avatar answered Sep 21 '22 20:09

Zarek