Home > SharePoint 2010 > Reverse-engineer SharePoint fields, content types and list instance—Part3

Reverse-engineer SharePoint fields, content types and list instance—Part3

Reverse-engineer SharePoint fields, content types and list instance—Part1

Reverse-engineer SharePoint fields, content types and list instance—Part2

Reverse-engineer SharePoint fields, content types and list instance—Part3

Reverse-engineer SharePoint fields, content types and list instance—Part4

In Part 1 and Part 2 of this series, I demonstrate how to reverse engineer SharePoint fields, content types. In this post I will cover how to include lookup fields in the content type and create list instance using these content types. You can download the updated source code here.

Firstly, I will cover how to create list instance and bind the custom content type to the custom list.

1. Create a custom list using list Instance item in visual studio and select custom list.

2. In the feature receiver add the Department content type to Department list and remove the item content type.

C#

AddContentTypeToList(web, “Department”, ” Department”);


private void AddContentTypeToList(SPWeb web,string listName, string contentTypeName)
{
SPList list = web.Lists.TryGetList(listName);
list.OnQuickLaunch = true;
list.ContentTypesEnabled = true;
list.Update();
SPContentType employeeContentType = web.ContentTypes[contentTypeName];
list.ContentTypes.Add(employeeContentType);
list.ContentTypes[“Item”].Delete();
list.Update();
}

Next, I will cover how to create the lookup fields. The difference between creating a normal field and lookup fields is that you need to create the lookup fields after the lists are created. This is because the lookup fields references fields from the foreign list.

1. In your solution, you need to create a feature that deploys the list before deploying the lookup fields.

2. You need to write the following code in the feature receiver to add the lookup columns in the ContentType.

C#


//add the lookup fields
SPFieldLookup departmentField = EnsureLookupField(currentWeb, “YBBESTDepartment”, currentWeb.Lists[“DepartmentList”].ID, “Title”);
//add to the content types
SPContentType employeeContentType = currentWeb.ContentTypes[“Employee”];
//Add the lookup fields as SPFieldLink
employeeContentType.FieldLinks.Add(new SPFieldLink(departmentField));
employeeContentType.Update(true);


private static SPFieldLookup EnsureLookupField(SPWeb currentWeb, String sFieldName, Guid LookupListID, String sLookupField)
{
//add the lookup fields
SPFieldLookup lookupField = null;
try
{
lookupField = currentWeb.Fields[sFieldName] as SPFieldLookup;
}
catch (Exception e)
{
}
if (lookupField == null)
{
currentWeb.Fields.AddLookup(sFieldName, LookupListID, true);
currentWeb.Update();
lookupField = currentWeb.Fields[sFieldName] as SPFieldLookup;
lookupField.LookupField = sLookupField;
lookupField.Group = “YBBEST”;
lookupField.Required = true;
lookupField.Update();
}
return lookupField;
}

3. If you would like to include more than fields from the foreign list, you can so using the code below , however there are limitations on what fields can be included in the master list.You can read my previous blog post for details.

C#


//Add dependent fields from foreign list.
SPFieldLink departmentAddressSPFieldLink = EnsureDependentLookup(currentWeb, departmentField, “DepartmentAddress”, “DepartmentLocation”);
employeeContentType.FieldLinks.Add(departmentAddressSPFieldLink);
employeeContentType.Update(true);


private static SPFieldLink EnsureDependentLookup(SPWeb currentWeb,SPFieldLookup policyField, string fieldName, string fieldNameToLookup)
{
SPFieldLookup dependentLookup = null;
currentWeb.Fields.AddDependentLookup(fieldName, policyField.Id);
currentWeb.Update();
dependentLookup = currentWeb.Fields[fieldName] as SPFieldLookup;
dependentLookup.LookupField = fieldNameToLookup;
dependentLookup.Group = “YBBEST”;
dependentLookup.Update();
SPFieldLink policyNamesFieldLink = new SPFieldLink(dependentLookup);
policyNamesFieldLink.DisplayName = fieldNameToLookup;
return policyNamesFieldLink;
}

You can download the updated source code here.

References:

SPFieldLookup Class

Advertisements
Categories: SharePoint 2010
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: