In this post I will showing you different code snippets that may be useful when developing SharePoint applications. The code accomplished various things and I will give you an idea how you may want to use them in your applications. The code is posted “AS IS” with no warranties and confers no rights.
Determining if a content type is published
The Content Type Publishing feature in SharePoint 2010 is very useful for the management and reuse of content types. It enables you to define a content type on one site collection and publish the content types to other site collections. More information: Content Type Publishing in SharePoint 2010.
Typically, the published content types are read only and can only be inherited from. So you may need some code to determine if a content type is published. The following code does this by examining the SPContentType.XmlDocuments collection. If the collection contains an XML document with a key of “Microsoft.SharePoint.Taxonomy.ContentTypeSync”, then it is either a published content type or derived from one. In order to decipher which is which, then you must compare the content type ID that is listed as an attribute in the XML document with the content type you are interrogating. If the two match then the content type has been published from the publishing hub.
public static bool IsContentTypePublished(SPContentType contentType)
Retrieving schema information for SharePoint built-In fields
Many developers use Visual Studio to develop and deploy SharePoint solutions that create document libraries and content types. More information: Creating custom content types. The definitions for these can contain FieldRef elements to existing built-in SharePoint fields and developers need a way to obtain information from the schema xml definition for the field. There are tools you can buy or download for free to obtain this information. However, you can use the simple code to run in a console application to obtain this information given a site URL and the internal name of the field. The code uses reflection to obtain the GUID of the built-in field using the internal name and then uses the GUID to obtain the schema.
public static string GetSchemaXmlForBuiltInField(string siteURL, string fieldName)
var result = from f in typeof(SPBuiltInFieldId).GetFields()
if (result != null && result.Count() == 1)
using (SPSite site = new SPSite(siteURL))
Setting default values for metadata fields and locations
A nice feature in SharePoint 2010 is being able to set default values for fields per location or folder. This is accessible from the list settings page under “column default value settings”.
A scenario may arise where you might want to have different default values for a metadata field per folder. Setting a default value for a managed metadata field is much different than other types of SharePoint fields. The following code shows how to do this using the Microsoft.Office.DocumentManagement.MetadataDefaults class. The method takes the URL to the site, list name, metadata field name and the relative URL to the folder. The code can be modified to accept the term group name, term set name and the default term you want to use. This example shows hard coded values these.
public static void AddDefaultMetaDataForLocation(string siteUrl,
using (SPSite site = new SPSite(siteUrl))
SPList list = web.Lists[listName];
TaxonomySession sessions = new TaxonomySession(web.Site);
string defaultValueText = "-1;#" + defaultValue.Labels.Value +
MetadataDefaults defaults = new MetadataDefaults(list);
defaults.SetFieldDefault(folder, metadataFieldName, defaultValueText);
More Code Tips Soon
SharePoint has many features and figuring out how to use these features in your application can take time. Hopefully these code tips can help speed up that process for you. I will try to post more of these in the future.