Programmatically create lookup field with picker

Jun 4, 2012 at 3:35 PM

Hi all,

 

Thank you very much for this great feature.

Is there someone who could tell me how to create and configure a field of type "LookupFieldWithPickerMulti" programmatically?

 

Thank you very much,

 

Yanick.

Jun 7, 2012 at 4:31 PM

Hi

I wanted to do the same thing. The way I am currently doing it is to use the Fields.AddFieldAsXml() method. I pass into this method the output from the method below. The strFieldType is basically "LookupFieldWithPicker".

I reached this solution using the following - http://blogs.msdn.com/b/syedi/archive/2009/12/04/programmatically-creating-a-field-column-from-the-custom-field-type-in-wss-3-0-moss.aspx

 

internal static string GetCreateFieldAsXml(Guid oId, string strDisplayName, bool Required, string strFieldType, string devName, string displayName)
        {
            XmlElement element = new XmlDocument().CreateElement("Field"); // Creating the �Field� element for the Inner XML schema

            element.SetAttribute("ID", oId.ToString()); // Setting the GUID of the field from value passed
            element.SetAttribute("Type", strFieldType); // Setting the Type name registered in the �Fldtypes*.xml� file
            element.SetAttribute("Name", devName); // Setting the Field name registered in the �Fldtypes*.xml� file
            element.SetAttribute("DisplayName", displayName); // Any unique Display Name 
            element.SetAttribute("Required", Required.ToString());

            return element.OuterXml; // Returning the OuterXML to create the field as XML
        }

Mostly you can cast the resulting field to the standard lookup type and set the properties this way (including the multiple selection part).

Hope this helps.

 

Zak

Jun 16, 2012 at 7:47 AM

Thanks a lot.

Here the code to create a lookup with picker field in a feature activation event (the code should be proper but works fine for me).

I hope it will help somebody.

Yanick.

 

using (SPWeb web = site.RootWeb)
{
    web.AllowUnsafeUpdates = true;

    SPList myList = site.RootWeb.Lists["MyList"] as SPList;
    SPList myTargetList = site.RootWeb.Lists["MyTargetList"] as SPList;
    String lookupFieldXML = createLookupWithPicker(Guid.NewGuid(), false, "MyLookpupField",
        "My Lookpup Field", true, false, myTargetList, "Title", 
        SPRelationshipDeleteBehavior.None, 5000, 
        new String[] { "FileLeafRef", "Title", "_dlc_DocId", "Created", "Author", "Modified", "_UIVersionString" });

    myList.Fields.AddFieldAsXml(lookupFieldXML);
    myList.Update();
                
    web.Update();
    web.AllowUnsafeUpdates = false;
}  


private static string createLookupWithPicker(Guid oId, 
        Boolean required, String devName, String displayName, Boolean multipleValues,
        Boolean enforceUniqueValues, SPList targetList, String showFieldName, SPRelationshipDeleteBehavior deleteBehavior,
        Int32 maxSearchResults, String[] searchableFields)
{
    XmlDocument document = new XmlDocument();
    XmlElement fieldElement = document.CreateElement("Field"); // Creating the Field element for the Inner XML schema

    // define field attributes
    fieldElement.SetAttribute("ID", oId.ToString());
    fieldElement.SetAttribute("Type", multipleValues ? "LookupFieldWithPickerMulti" : "LookupFieldWithPicker");
    fieldElement.SetAttribute("StaticName", devName);
    fieldElement.SetAttribute("Name", devName);
    fieldElement.SetAttribute("DisplayName", displayName);
    fieldElement.SetAttribute("Required", required.ToString().ToUpper());
    fieldElement.SetAttribute("EnforceUniqueValues", enforceUniqueValues.ToString().ToUpper());
    fieldElement.SetAttribute("WebId", targetList.ParentWeb.ID.ToString());
    fieldElement.SetAttribute("List", targetList.ID.ToString());
    fieldElement.SetAttribute("ShowField", showFieldName);
    fieldElement.SetAttribute("Mult", multipleValues.ToString().ToUpper());
    fieldElement.SetAttribute("Sortable", Boolean.FalseString.ToUpper());
    fieldElement.SetAttribute("UnlimitedLengthInDocumentLibrary", Boolean.TrueString.ToUpper());
    fieldElement.SetAttribute("RelationshipDeleteBehavior", deleteBehavior.ToString());
    fieldElement.SetAttribute("RowOrdinal", "0");
    fieldElement.SetAttribute("Version", "3");

    // define field properties
    XmlElement custElement = document.CreateElement("Customization");
    fieldElement.AppendChild(custElement);

    XmlElement propsElement = document.CreateElement("ArrayOfProperty");
    custElement.AppendChild(propsElement);

    // set the Searchable Fields property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("SearchableFields"));
        propElement.AppendChild(nameElement);
                
        StringBuilder builder = new StringBuilder();
        foreach (String fieldName in searchableFields)
        {
            builder.Append(fieldName).Append(",");
        }
        // remove last ","
        builder.Remove(builder.Length - 1, 1);
                    
        propElement.InnerXml = propElement.InnerXml
            + "<Value xmlns:q1=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q1:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">" 
            + builder.ToString()
            + "</Value>";                 
    }

    // set the Custom Default Value property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("CustomDefaultValue"));
        propElement.AppendChild(nameElement);
                
        propElement.InnerXml = propElement.InnerXml 
                + "<Value xmlns:q2=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q2:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\" />";                 
    }

    // set the Max Search Results property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("MaxSearchResults"));
        propElement.AppendChild(nameElement);
               
        propElement.InnerXml = propElement.InnerXml
            + "<Value xmlns:q3=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q3:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">" 
            + maxSearchResults.ToString()
            + "</Value>";
               
    }

    // set the Entity Editor Rows property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("EntityEditorRows"));
        propElement.AppendChild(nameElement);
                
        propElement.InnerXml = propElement.InnerXml
                + "<Value xmlns:q4=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q4:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">1</Value>";                    
                
    }

    // set the Custom Version property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("CustomVersion"));
        propElement.AppendChild(nameElement);
                
        propElement.InnerXml = propElement.InnerXml
            + "<Value xmlns:q5=\"http://www.w3.org/2001/XMLSchema\" p4:type=\"q5:string\" xmlns:p4=\"http://www.w3.org/2001/XMLSchema-instance\">1.1.0.0</Value>";                                
    }

    // set the Custom Custom Lookup List property
    {
        XmlElement propElement = document.CreateElement("Property");
        propsElement.AppendChild(propElement);
                
        XmlElement nameElement = document.CreateElement("Name");
        nameElement.AppendChild(document.CreateTextNode("CustomLookupList"));
        propElement.AppendChild(nameElement);                
    }

    // Returning the OuterXML to create the field as XML
    return fieldElement.OuterXml; 
}