SharePoint 4 Developers

Additional reference guide in .NET / SharePoint Development

Migration of Old Posts

The link is no longer available, so I am going to migrate all my old posts (since 2009) gradually.

Hi there,

As you probably know, all the Windows Live blogs came to an end. Just to let you know, the link is no longer available. Sad smile

The Windows Live Team gave notice beforehand and offered a migration without efforts to WordPress.

In my case I have decided to migrate all my posts to here, and I am going to do that by the next weeks.

Please be patient in this transition as I am going to move all my old posts (since 2009) gradually. Smile



Marcel Medina

Click here to read the same content in Portuguese.

Creating Document Sets Programmatically

In this post I will show how to create document sets programmatically, by exploring the assembly Microsoft.Office.DocumentManagement.dll, available at the directory \14\ISAPI.

Hi folks,

I have been requested to create lots of document sets in replacement of folders recently. It is easy to create document sets manually, but when you need to create lots of them, the best solution is to do it programmatically.

Document sets are more than simple folders. They hold metadata, which is changing the way users are working and they are taking advantage of it.

As a quick note, in this post I will show how to create document sets programmatically, by exploring the assembly Microsoft.Office.DocumentManagement.dll, available at the directory \14\ISAPI\.

The Solution

Basically this solution shows how to create a document set and add it to a Document Library.

Note: 2 content types were created manually for demonstration purposes: Word Template and Excel Spreadsheet Template.

Check this out:

Code Snippet
  1. static void Main(string[] args)
  2. {
  3.     var url = ConfigurationManager.AppSettings["Url"].ToString();
  4.     var sub = ConfigurationManager.AppSettings["Sub"].ToString();
  5.     var library = ConfigurationManager.AppSettings["Library"].ToString();
  7.     using (SPSite site = new SPSite(url))
  8.     {
  9.         using (SPWeb web = (sub == "*") ? site.RootWeb : site.OpenWeb(sub))
  10.         {
  11.             string docsetName = "Document Set Test";
  12.             SPContentType ctype = null;
  14.             if (web.ContentTypes[docsetName] == null)
  15.             {
  16.                 // Creating the document set (content type)
  17.                 ctype = new SPContentType(web.ContentTypes["Document Set"], web.ContentTypes, "Document Set Test");
  18.                 ctype.FieldLinks.Add(new SPFieldLink(web.Fields["Author"]));
  19.                 ctype.Group = "Test Content Types";
  20.                 web.ContentTypes.Add(ctype);
  22.                 // Getting the document set (content type)
  23.                 DocumentSetTemplate docsetTemplate = DocumentSetTemplate.GetDocumentSetTemplate(ctype);
  25.                 // Setting the content types
  26.                 docsetTemplate.AllowedContentTypes.Remove(web.ContentTypes["Document"].Id);
  27.                 docsetTemplate.AllowedContentTypes.Add(web.ContentTypes["Word Template"].Id);
  28.                 docsetTemplate.AllowedContentTypes.Add(web.ContentTypes["Excel Spreadsheet Template"].Id);
  30.                 // Sharing fields
  31.                 docsetTemplate.SharedFields.Add(web.Fields["Author"]);
  33.                 // Displaying fields
  34.                 docsetTemplate.WelcomePageFields.Add(web.Fields["Author"]);
  36.                 // Adding default document
  37.                 FileStream wordFile = File.OpenRead(Path.GetFullPath(@"DocumentSet\Default.dotx"));
  38.                 byte[] binWordFile = new byte[wordFile.Length];
  39.                 wordFile.Read(binWordFile, 0, binWordFile.Length);
  41.                 docsetTemplate.DefaultDocuments.Add("Default.dotx", web.ContentTypes["Word Template"].Id, binWordFile);
  43.                 // Updating the document set (content type),    
  44.                 docsetTemplate.Update(true);
  45.                 ctype.Update();
  46.             }
  48.             ctype = ctype jQuery15207137137458194047_1344344305332 web.ContentTypes[docsetName];
  50.             if (web.Lists.TryGetList(library) == null)
  51.             {
  52.                 // Creating document library
  53.                 Guid libraryGuid = web.Lists.Add(library, "", SPListTemplateType.DocumentLibrary);
  54.                 SPDocumentLibrary list = (SPDocumentLibrary)web.Lists[libraryGuid];
  56.                 // Setting properties
  57.                 list.OnQuickLaunch = true;
  58.                 list.ContentTypesEnabled = true;
  59.                 list.EnableFolderCreation = false;
  61.                 // Defining content types
  62.                 list.ContentTypes.Delete(list.ContentTypes["Document"].Id);
  63.                 list.ContentTypes.Add(ctype);
  64.                 list.Update();
  66.                 System.Collections.Hashtable properties = new System.Collections.Hashtable();
  67.                 properties.Add("DocumentSetDescription", "Just an example"); //InternalName
  68.                 properties.Add("_Author", "MM"); //InternalName
  70.                 // Creating the document set
  71.                 DocumentSet.Create(list.RootFolder, "DocSet1", list.ContentTypes.BestMatch(ctype.Id), properties, true);
  72.             }
  73.         }
  74.     }
  75. }

Note: A Document Library is created dynamically for demonstration purposes.

Download the solution here.

I hope it helps. Smile



Marcel Medina

Click here to read the same content in Portuguese.

Programmatically Setting Conditional Formatting

Learn how to how to apply conditional formatting to views manually and programmatically by using SharePoint Designer 2010 and Visual Studio 2010 respectively.

Hi folks,

Today I want to talk about conditional formatting, which allows you to apply HTML styles to views depending on the criteria specified. This is not something new in SharePoint, in MOSS 2007 this already existed.

In this post I am going to demonstrate how to apply conditional formatting manually and programmatically, so stay tuned!

Conditional formatting in SharePoint Designer 2010

Let’s take the Grades list below as an example. Basically the idea is to implement a range of colours against the column Grade that differentiates grades. Based on the grade these colours will vary.

Figure 1 – Grades List

By using the SharePoint Designer 2010, this customisation is easy. Go to the Grades list in SharePoint Designer 2010 and open up the view called “All Items”, according to the Figure 2:

Figure 2 – All Items view

Interestingly the view can be visualised in both ways (Design and Code), which allows an easy customisation. By following the steps according to the Figure 3 the conditional formatting will be applied against the column Grade.

Figure 3 – Conditional Formatting in SharePoint 2010

Immediately after the step 3 above, you need to create a condition criteria that will do the task we need by applying a style in the Grades List.

Figure 4 – Condition Criteria

According to the condition criteria above, set the background colour to be Green.

Note: Different colours need to be applied against different condition criteria.

Figure 5 – Background colour

After configuring all the condition criteria, the conditional formatting can be visualised in different colours, according to the Figure 6:

Figure 6 – Conditional Formatting colours

The conditional formatting bar is very clear, and summarises the criteria you have specified. It allows you to configure the styles at any time. Pretty handy!

In the end you will get your the Grades List set according to the Figure 7:

Figure 7 – Conditional Formatting applied to the Grades List

Conditional formatting in Visual Studio 2010

So far you have seen how to set conditional formatting manually. Now you will see how to set this up programmatically!

First of all you need to identify the Xsl section (from Figure 6), copy all the content from this section to a xsl file, according to the code below:

Code Snippet
  1. <xsl:stylesheet xmlns:x="" xmlns:d="" version="1.0" exclude-result-prefixes="xsl msxsl ddwrt" xmlns:ddwrt="" xmlns:asp="" xmlns:__designer="" xmlns:xsl="" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:SharePoint="Microsoft.SharePoint.WebControls" xmlns:ddwrt2="urn:frontpage:internal" xmlns:o="urn:schemas-microsoft-com:office:office">
  2.   <xsl:include href="/_layouts/xsl/main.xsl"/>
  3.   <xsl:include href="/_layouts/xsl/internal.xsl"/>
  4.   <xsl:param name="AllRows" select="/dsQueryResponse/Rows/Row[$EntityName = '' or (position() &gt;= $FirstRow and position() &lt;= $LastRow)]"/>
  5.   <xsl:param name="dvt_apos">&apos;</xsl:param>
  6.   <xsl:template name="FieldRef_printTableCell_EcbAllowed.Grade" match="FieldRef[@Name='Grade']" mode="printTableCellEcbAllowed" ddwrt:dvt_mode="body" ddwrt:ghost="" xmlns:ddwrt2="urn:frontpage:internal">
  7.     <xsl:param name="thisNode" select="."/>
  8.     <xsl:param name="class" />
  9.     <td>
  10.       <xsl:attribute name="style">
  11.         <xsl:if test="normalize-space($thisNode/@Grade) = 'A'" ddwrt:cf_explicit="1" xmlns:ddwrt="">background-color: #009900;</xsl:if>
  12.         <xsl:if test="normalize-space($thisNode/@Grade) = 'B'" ddwrt:cf_explicit="1" xmlns:ddwrt="">background-color: #66FF33;</xsl:if>
  13.         <xsl:if test="normalize-space($thisNode/@Grade) = 'C'" ddwrt:cf_explicit="1" xmlns:ddwrt="">background-color: #FFFF00;</xsl:if>
  14.         <xsl:if test="normalize-space($thisNode/@Grade) = 'D'" ddwrt:cf_explicit="1" xmlns:ddwrt="">background-color: #FF9900;</xsl:if>
  15.         <xsl:if test="normalize-space($thisNode/@Grade) = 'E'" ddwrt:cf_explicit="1" xmlns:ddwrt="">background-color: #FF0000;</xsl:if>
  16.       </xsl:attribute>
  18.       <xsl:if test="@ClassInfo='Menu' or @ListItemMenu='TRUE'">
  19.         <xsl:attribute name="height">100%</xsl:attribute>
  20.         <xsl:attribute name="onmouseover">OnChildItem(this)</xsl:attribute>
  21.       </xsl:if>
  22.       <xsl:attribute name="class">
  23.         <xsl:call-template name="getTDClassValue">
  24.           <xsl:with-param name="class" select="$class" />
  25.           <xsl:with-param name="Type" select="@Type"/>
  26.           <xsl:with-param name="ClassInfo" select="@ClassInfo"/>
  27.         </xsl:call-template>
  28.       </xsl:attribute>
  29.       <xsl:apply-templates select="." mode="PrintFieldWithECB">
  30.         <xsl:with-param name="thisNode" select="$thisNode"/>
  31.       </xsl:apply-templates>
  32.     </td>
  33.   </xsl:template>
  34. </xsl:stylesheet>

Note: The SharePoint Designer 2010 created a subsection called xsl:attribute to store the conditional formatting you have specified (Lines 10 to 16).

In this example I am creating a feature that is going to deploy the Grades list. So check the structure of the solution for you to have an idea:

Figure 8 – Conditional Formatting solution

Note: The xsl files in SharePoint 2010 must be deployed under the directory Layouts\Xsl.

To deploy the Grades list I have coded the FeatureActivated method (available in the Event Receiver of the feature):

Code Snippet
  1. public override void FeatureActivated(SPFeatureReceiverProperties properties)
  2. {
  3.     SPWeb web = (SPWeb)properties.Feature.Parent;
  5.     string grades = "Grades";
  7.     if (web.Lists.TryGetList(grades) == null)
  8.     {
  9.         // Creating list
  10.         Guid listGuid = web.Lists.Add(grades, "", SPListTemplateType.GenericList);
  11.         SPList list = web.Lists[listGuid];
  12.         list.OnQuickLaunch = true;
  14.         // Configuring fields
  15.         SPField title = list.Fields["Title"];
  16.         title.Title = "Name";
  17.         title.Update(true);
  19.         list.Fields.Add("Grade", SPFieldType.Text, true);
  21.         // Updating view
  22.         SPView mainView = list.Views[0];
  23.         mainView.ViewFields.DeleteAll();
  24.         mainView.ViewFields.Add("Attachments");
  25.         mainView.ViewFields.Add("LinkTitle");
  26.         mainView.ViewFields.Add("Grade");
  27.         mainView.XslLink = "ConditionalFormattingXsl/grades.xsl";
  28.         mainView.Update();
  30.         // Saving changes
  31.         list.Update();
  32.     }
  33. }

Note: Pay attention to the line 27. That’s what I am talking about! Beautiful.

Once you have deployed the solution, activate it and you will get the Grades list created with the Conditional Formatting applied automatically.

Figure 9 – Feature created

Download the solution here.

To make things easier, always use SharePoint Designer to set up conditional formatting on your views. Then copy the Xsl section and paste to a xsl file. It does the trick.

I hope it helps.



Marcel Medina

Click here to read the same content in Portuguese.