Search This Blog

Monday, December 1, 2014

Programatically Create Document Set with SharePoint 2010

1-
SharePoint 2010 has a new concept of a Document Set, which is essentially a folder-like container allowing you to group your files in and perform various activities on the set, such as trigger workflows, submit entire set to a record center and so on.
In this article, I will focus on how you can create a new document set programatically, which is handy if you need to pre-create a collection of document sets as part of your solution deployment.
I will assume, you have a sandbox site of a Team Site template on it; in my case the URL of my sandbox site is
First, we need to ensure our site has a Document Set feature enabled:
From click Site Actions-> Site Settings -> Site Collection features -> ensure you have activated Document Set feature.
Now, let’s take a look at what’s involved in provisioning an instance of a document set programmatically.
1. In Visual Studio 2010, create a blanks SharePoint 2010 Solution.
2. Add a Feature to your solution structure, right click on the feature name to add anFeature Receiver.
3. Let’s add the reference to a document set assemblies in Visual Studio. Right click onReferences in your Visual Studio solution structure and Add a reference using aSharePoint tab.
The reference we’ll add is: Microsoft.Office.DocumentManagement
Your structure will look like this:
4. Now, switch to your Feature1.EventReceiver.cs and add the following namespace references to the class:
using System.Collections;
using Microsoft.Office.DocumentManagement.DocumentSets;
5. Replace the commented section for FeatureActivated block, with the following code:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
SPWeb web = properties.Feature.Parent as SPWeb;
SPList sharedDocs = web.Lists["Shared Documents"];
if (sharedDocs != null)
{
    SPContentType docSetCT = web.ContentTypes["Document Set"];
    if (docSetCT != null)
    {
        Hashtable props = new Hashtable();
        props.Add("DocumentSetDescription", "New Doc Set");
        DocumentSet docSet = DocumentSet.Create(sharedDocs.RootFolder, 
"New Doc Set", docSetCT.Id, props, true);
        docSet.Item.ProgId = "SharePoint.DocumentSet";
        docSet.Item.Update();
    }
}
}
Above, we get a hold of the site where our custom feature is activated and access theShared Documents library on the site. Then, we ensure the Document Set content type is available on the site (it’s not going to be available if Document Set site collection was not activated, which we did activate earlier). Then, we create a new document set with the default name and description properties.
The last piece of code is assignment of ProgId. This is actually very important part. If we don’t assign ProgId, the document set will still be created but it will look like a folder and your users will be confused with the UI and other functionality, such as welcome page of the document set will be just a plain folder view.
6. Deploy the solution with Visual Studio and navigate to your Team Site’s Shared Documents library
After the set has been added with our feature receiver, the library will have in the view:
Enjoy!

                                  OR

2-Create Document Set in SharePoint 2010 programmatically using VS 2012 Using visualwebpart 


Create Document Set in SharePoint 2010 programmatically using VS 2012
Introduction
Today, in this article let’s play around with one of the interesting and most useful concept in SharePoint 2010.

Question: What is DocumentSet?
In simple terms “Document set helps to manage group of documents as a single entity.”.

Step 1: Open SharePoint 2010 central administration and navigate to specific site

Step 2: Open up visual studio 2012 and try to select sharepoint visualwebpart project


Step 3: Select deploy as a farm solution and click on next – button.

Step 4: The complete code of visualwebpart1usercontrol.ascx looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
</p>
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1.ascx.cs" Inherits="DocumentSetApp.VisualWebPart1.VisualWebPart1" %>
<center>
 <div>
 <table style="text-align:center">
 <tr>
 <td colspan="2">
 <asp:Label ID="Label1" runat="server" Text="DocumentSet - SharePoint 2010 Programatically using VS 2012"
 Font-Bold="true" Font-Size="Large" Font-Names="Verdana" ForeColor="Maroon"></asp:Label>
 </td>
 </tr>
 <tr>
 <td>
 <asp:Label ID="Label2" runat="server" Text="Please Enter List Name" Font-Size="Large"
 Font-Names="Verdana" Font-Italic="true"></asp:Label>
 </td>
 <td>
 <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td>
 <asp:Label ID="Label3" runat="server" Text="Please Enter DocumentSet Name" Font-Size="Large"
 Font-Names="Verdana" Font-Italic="true"></asp:Label>
 </td>
 <td>
 <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
 </td>
 </tr>
 <tr>
 <td colspan="2">
 <asp:Button ID="Button1" runat="server" Text="Create Document Set" Font-Names="Verdana"
 Width="174px" BackColor="Orange" Font-Bold="True"
 OnClick="Button1_Click" />
 </td>
 </tr>
 <tr>
 <td colspan="2">
 <asp:Label ID="Label5" runat="server" Font-Bold="true" Font-Names="Verdana"></asp:Label>
 </td>
 </tr>
 </table>
</div>
</center>
<p style="text-align: justify;">
Step 5: The complete code of visualwebpart1usercontrol.ascx.cs looks like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint;
using System;
using System.Collections;
using System.ComponentModel;
using System.Web.UI.WebControls.WebParts;
 
namespace DocumentSetApp.VisualWebPart1
{
 [ToolboxItemAttribute(false)]
 public partial class VisualWebPart1 : WebPart
 {
 // Uncomment the following SecurityPermission attribute only when doing Performance Profiling using
 // the Instrumentation method, and then remove the SecurityPermission attribute when the code is ready
 // for production. Because the SecurityPermission attribute bypasses the security check for callers of
 // your constructor, it's not recommended for production purposes.
 // [System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Assert, UnmanagedCode = true)]
 public VisualWebPart1()
 {
 }
 
protected override void OnInit(EventArgs e)
 {
 base.OnInit(e);
 InitializeControl();
 }
 
protected void Page_Load(object sender, EventArgs e)
 {
 TextBox1.Focus();
 }
 
protected void Button1_Click(object sender, EventArgs e)
 {
 if (string.IsNullOrEmpty(TextBox1.Text) || string.IsNullOrEmpty(TextBox2.Text))
 {
 Label5.Text = "Please Enter Some Values";
 Label5.ForeColor = System.Drawing.Color.Red;
 }
 else
 {
 try
 {
 SPWeb web = SPContext.Current.Web;
 SPList list = web.Lists[TextBox1.Text];
 if (list != null)
 {
 SPContentType contentType = web.ContentTypes["Document Set"];
 if (contentType != null)
 {
 var hashTable = new Hashtable();
 hashTable.Add("DocumentSet", "New Doc Setting");
 hashTable.Add("CreatedBy", SPContext.Current.Web.CurrentUser.Name);
 hashTable.Add("ModifiedBy", SPContext.Current.Web.CurrentUser.Name);
 DocumentSet documentSet = DocumentSet.Create(list.RootFolder, TextBox2.Text, contentType.Id,
 hashTable, true);
 documentSet.Item.ProgId = "SharePoint.DocumentSet";
 documentSet.Item.Update();
 }
 }
 
}
 
catch (SPException ex)
 {
 
Label5.Text = ex.Message;
 }
 
Label5.Text = TextBox2.Text + " Document Set Created Successfully";
 Label5.ForeColor = System.Drawing.Color.Green;
 TextBox1.Text = string.Empty;
 TextBox2.Text = string.Empty;
 }
 
}
 
}
}
 
&nbsp;

Step 6: Deploy the solution file and add the created webpart to sharepoint site

Step 7: The output of the application looks like this


Step 8: Entering document set data output of the application looks like this


Step 9: Document set create output of the application looks like this







 3-                                                       OR 
You can create a document set using sharePoint object model, SharePoint Client OM and also using SharePoint 2010 web services.
This example shows how to create document sets using SharePoint object model.
public static void CreateDocumentSetContentType(string libraryUrl)
{
using (SPSite site = new SPSite(libraryUrl))
{
using (SPWeb web = site.OpenWeb())
{
//create the new document set contenttype
SPContentType newDocumentSet = web.ContentTypes.Add (new SPContentType(web.ContentTypes[“Document Set”],web.ContentTypes,
“MydocumentSet”));
//get a an instance of DocumentSetTemplate for the new document set
DocumentSetTemplate newDocumentSetTemplate = DocumentSetTemplate.GetDocumentSetTemplate(newDocumentSet );
//add allowable content types
newDocumentSetTemplate.AllowedContentTypes.Add
(web.ContentTypes[“Document”].Id);
//add a shareable property
newDocumentSetTemplate.SharedFields.Add(newDocumentSetContentType.Fields[“Module”]);
newDocumentSetTemplate.Update(true);
newDocumentSet .Update();
web.Update();
}
}
}


1 comment:

  1. Hello, do you know if there is a way to customize the welcome page of the document set with C# and CSOM ?

    ReplyDelete