2

This is what I've achieved do far:

var listCreationInfo = new ListCreationInformation();
listCreationInfo.Title = "New List";
listCreationInfo.TemplateType = (int)ListTemplateType.CustomGrid;
List list = web.Lists.Add(listCreationInfo);
list.Update();
ctxt.ExecuteQuery();
Field parent = list.Fields.AddFieldAsXml(
    @"<Field Type='Lookup' DisplayName='ParentContentType' 
    Required='FALSE' EnforceUniqueValues='FALSE'
    List='" + list.TemplateFeatureId + @"'
    ShowField='Title' UnlimitedLengthInDocumentLibrary='FALSE' 
    RelationshipDeleteBehavior='None' 
    StaticName='ParentContentType' Name='ParentContentType'/>",
    true, AddFieldOptions.DefaultValue);

But I am not able to set the Get information from: value for this lookup field. Can anyone please suggest how to achieve it?

2 Answers 2

2

Try This:-

public static void AddLookupField()
{
            string lookupSchema = @"<Field Type='Lookup' DisplayName='mylookup2' 
                                    Required='FALSE' EnforceUniqueValues='FALSE'
                                    List='{70d6098c-6ba0-4e9e-b101-a60b88fc226a}' 
                                    ShowField='Title' UnlimitedLengthInDocumentLibrary='FALSE' 
                                    RelationshipDeleteBehavior='None' 
                                    StaticName='mylookup' Name='mylookup2'/>";

            ClientContext clientContext = new ClientContext("http://sharepoint10");
            List list = clientContext.Web.Lists.GetByTitle("listtitle");
            FieldCollection fields = list.Fields;

            clientContext.Load(list);
            clientContext.Load(fields);

            clientContext.ExecuteQuery();

            Field lookupField = fields.AddFieldAsXml(lookupSchema, true, AddFieldOptions.AddToDefaultContentType);
            lookupField.Update();

            clientContext.Load(lookupField);
            clientContext.ExecuteQuery();


}
2
  • Hey thanks @sharique, my issue got resolved. I used the list's GUID and it worked perfectly. :)
    – Abhishek
    Feb 17, 2015 at 6:10
  • you are welcome @CodeKaro Feb 17, 2015 at 7:14
0

The "List" attribute of the <Field> element should specify the list from which you wish to get information.

It should be the internal name of a list on the site; the internal name is in the format of a URL fragment, such as "Lists/MyList".

2
  • Hi @thriggle, thanks for the response. I have seen at some places that "List='{E0D2646A-63BE-4010-ABC7-4A5E2B8EF0E7}'" has been added. What does that mean?
    – Abhishek
    Feb 16, 2015 at 17:10
  • Hi @CodeKaro, you can use a list's GUID instead of the list's URL if you want to refer to a list somewhere in the same site collection but not necessarily in the same site. I believe you'll need {curly braces} around the GUID of the desired list.
    – Thriggle
    Feb 16, 2015 at 20:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.