Monday, July 23, 2012

Programming with Content Types in SharePoint 2010

// Programming with Content Types.
using System;
using System.Runtime.InteropServices;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Linq;
namespace SPHOL302_Ex2.Features.Feature1
{  
    [Guid("8c019752-8354-430b-ba1c-bed7daac7c48")]
    public class Feature1EventReceiver : SPFeatureReceiver
    {
        // Uncomment the method below to handle the event raised after a feature has been activated.
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
            {
                SPContentType newAnnouncement = spWeb.ContentTypes.Cast<SPContentType>().FirstOrDefault(c => c.Name == "New Announcements");
                if (newAnnouncement != null)
                {
                    newAnnouncement.Delete();
                }
                SPField newField = spWeb.Fields.Cast<SPField>().FirstOrDefault(f => f.StaticName == "Team Project");
                if (newField != null)
                {
                    newField.Delete();
                }
                SPContentType myContentType = new SPContentType(spWeb.ContentTypes["Announcement"], spWeb.ContentTypes, "New Announcements");
                myContentType.Group = "Custom Content Types";
                spWeb.Fields.Add("Team Project", SPFieldType.Text, true);
                SPFieldLink projFeldLink = new SPFieldLink(spWeb.Fields["Team Project"]);
                myContentType.FieldLinks.Add(projFeldLink);
                SPFieldLink companyFieldLink = new SPFieldLink(spWeb.Fields["Company"]);
                myContentType.FieldLinks.Add(companyFieldLink);
                spWeb.ContentTypes.Add(myContentType);
                myContentType.Update();
            }
        }
        // Uncomment the method below to handle the event raised before a feature is deactivated.
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            using (SPWeb spWeb = properties.Feature.Parent as SPWeb)
            {
                SPContentType myContentType = spWeb.ContentTypes["New Announcements"];
                spWeb.ContentTypes.Delete(myContentType.Id);
                spWeb.Fields["Team Project"].Delete();
            }
        }
        // Uncomment the method below to handle the event raised after a feature has been installed.
        //public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised before a feature is uninstalled.
        //public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        //{
        //}
        // Uncomment the method below to handle the event raised when a feature is upgrading.
        //public override void FeatureUpgrading(SPFeatureReceiverProperties properties, string upgradeActionName, System.Collections.Generic.IDictionary<string, string> parameters)
        //{
        //}
    }
}
----------------------------------------------------------------------------------------------------------------------

ECM(Enterprise Content Management) Create Taxonomy Programatically in SharePoint 2010




//Application Management -> Service Applications -> Manage service applications -> Managed Metadata Service
using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.WebControls;
namespace ECM_CreateTaxonomy.Layouts.ECM_CreateTaxonomy
{
    public partial class CreateTaxonomy : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void createTaxonomyButton_Click(object sender, EventArgs e)
        {
            SPSite currentSite = SPContext.Current.Site;
            TaxonomySession session = new TaxonomySession(currentSite);
            TermStore termstore = session.TermStores["Managed Metadata Service"];
            Group plantsGroup = termstore.CreateGroup("Plants");
            TermSet flowersTermSet = plantsGroup.CreateTermSet("Flowers");
            Term tulipsTerm = flowersTermSet.CreateTerm("Tulips", 1033);
            Term orchidsTerm = flowersTermSet.CreateTerm("Orchids", 1033);
            Term daffodilsTerm = flowersTermSet.CreateTerm("Daffodils", 1033);
            Term vanillaTerm = orchidsTerm.CreateTerm("Vanilla", 1033);
            vanillaTerm.SetDescription("A common orchid whose pods are used in desserts", 1033);
            vanillaTerm.CreateLabel("Vanilla planifolia", 1033, false);
            vanillaTerm.CreateLabel("Flat-leaved vanilla", 1033, false);
            try
            {
                termstore.CommitAll();
                resultsLabel.Text = "Taxonomy created successfully";
            }
            catch (Exception ex)
            {
                resultsLabel.Text = "There was an error: " + ex.Message;
            }
        }
    }
}
-------------------------------------------------------------------------------------------------------------------------

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;
            }
        }
    }
}
-------------------------------------------------------------------------------------------------------------------