Monday, July 23, 2012

Create Document Set Programatically In SharePoint 2010





CreateDocumentSet.aspx
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ 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" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="CreateDocumentSet.aspx.cs" Inherits="ECM_CreateDocumentSet.Layouts.ECM_CreateDocumentSet.CreateDocumentSet" DynamicMasterPageFile="~masterurl/default.master" %>
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
</asp:Content>
<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
    <h1>Document Set Creation Demo</h1>
    <p>
        This page creates a new document set in the Shared Documents folder. Document
        sets enable you to manage multiple documents as one. For example, you can submit
        an entire document set as a record in a single operation. You can also apply
        metadata and permissions to every document in the set.  Before you
        can create and use document sets, you must enable the Document Sets feature at
        the site collection level. Then add the Document Set content type to the document
        library where you want to use them.
    </p>
    <p>
        Name: <asp:TextBox ID="nameTextbox" runat="server"></asp:TextBox>
    </p>
    <p>
        Description: <asp:TextBox ID="descriptionTextbox" runat="server"></asp:TextBox>
    </p>
    <asp:Button ID="createDocSetButton" OnClick="createDocSetButton_Click" runat="server" Text="Create Document Set" />
    <asp:Label ID="resultLabel" runat="server" Text=""></asp:Label>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
    ECM Document Set Creation
</asp:Content>
<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
    ECM Document Set Creation
</asp:Content>
---------------------------------------------------------------------------------------------------------------
CreateDocumentSet.aspx.cs
using System;
using System.Collections;
using Microsoft.Office.DocumentManagement.DocumentSets;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
namespace ECM_CreateDocumentSet.Layouts.ECM_CreateDocumentSet
{
    /// <summary>
    /// This application page creates a document set based on the default document set
    /// content type.
    /// </summary>
    /// <remarks>
    /// You must have enabled the site collection level Document Sets feature, and added
    /// the Document Set content type to the document library, before you can create
    /// document sets.
    /// </remarks>
    public partial class CreateDocumentSet : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void createDocSetButton_Click(object sender, EventArgs e)
        {
            //Get the Shared Documents document library
            SPWeb currentWeb = SPContext.Current.Web;
            SPDocumentLibrary sharedDocsLib = (SPDocumentLibrary)currentWeb.Lists["Shared Documents"];
            //You can use a hashtable to populate properties of the document set
            Hashtable docsetProperties = new Hashtable();
            docsetProperties.Add("Name", nameTextbox.Text);
            docsetProperties.Add("DocumentSetDescription", descriptionTextbox.Text);
            //Create the document set
            try
            {
                DocumentSet newDocSet = DocumentSet.Create(sharedDocsLib.RootFolder,
                    nameTextbox.Text, sharedDocsLib.ContentTypes["Document Set"].Id,
                    docsetProperties, true);
                resultLabel.Text = "Document set created";
            }
            catch (Exception ex)
            {
                resultLabel.Text = "An error occurred: " + ex.Message;
            }
        }
    }
}
-------------------------------------------------------------------------------------------------------------------

4 comments:

  1. This article is very good.informative style.. brilliant piece of advice.
    Manideep

    ReplyDelete
  2. It is incredible and informative knowledge. Impressive.
    Kiran

    ReplyDelete
  3. Wooh this is very informative article. Thank you so much..!
    Rakesh

    ReplyDelete
  4. Information was good,i like your post.
    Looking forward for more on this topic
    Lahir

    ReplyDelete