Showing posts with label SharePoint 2010 content types. Show all posts
Showing posts with label SharePoint 2010 content types. Show all posts

02/10/2013

Add Site Column to existing Content Type in code

How to add a new Site Column to an existing Content Type from code


This post describes how to create a new column, add it to an existing Content Type in an existing feature and last run an upgrade in PowerShell of the feature.

Feature.xml file:
<?xml version="1.0" encoding="utf-8" ?>
<Feature xmlns="
http://schemas.microsoft.com/sharepoint/" Version="2.0.1.0">
  <ElementManifests>
    <ElementManifest Location="NewColName\Elements.xml" />
  </ElementManifests> 
  <UpgradeActions>
    <VersionRange EndVersion="3.0.0.0">
      <ApplyElementManifests>
        <ElementManifest Location="NewColName\Elements.xml" />
      </ApplyElementManifests>
      <CustomUpgradeAction Name="AddFieldToContentType">
        <Parameters>
          <Parameter Name="FieldId">x-x-x-x-x</Parameter>
          <Parameter Name="ContentTypeId">xxxx</Parameter>
          <Parameter Name="PushDown">TRUE</Parameter>
        </Parameters>
      </CustomUpgradeAction>
    </VersionRange>
  </UpgradeActions>
</Feature>


The NewColName\Elements.xml contains the definition of the new column and shall be added in both the ElementManifest section and the UpgradeActions-ApplyElementManifest section. The ElementManifest section is used when deploying the feature the first time, and the UpgradeAction section is used when upgrading the feature. The new version of the feature is 2.0.1.0, and the upgrade action shall be active until version 3.0.0.0 (EndVersion attribute).
The CustomUpgradeAction refers to an upgrade action named "AddFieldToContentType". The action takes three parameters the new field ID the existing Content type ID and a parameter called PushDown, witch configures of the Content Type changes shall be pushed down to child Content Types.

The new column is defined in NewColName\Elements.xml:
<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="
http://schemas.microsoft.com/sharepoint/"> 
  <Field

    Name="NewColName"
    StaticName="NewColName"
    Type="Text"
    Required="FALSE"
    DisplayName="NewColName"
    Description="The new column"
    Group="NewGroup"
    ID="{x-x-x-x-x}"
    Overwrite="TRUE" OverwriteInChildScopes="FALSE" 
    SourceID="http://schemas.microsoft.com/sharepoint/v3"
    xmlns="http://schemas.microsoft.com/sharepoint/" />
</Elements>


Create an event receiver and override the FeatureUpgrading method, this method is invoked when the feature is upgraded (surprise!) and the upgrade action name is taken as a parameter:
public override void FeatureUpgrading(
  SPFeatureReceiverProperties properties,
  string upgradeActionName,
  System.Collections.Generic.IDictionary<string, string> parameters)
{
  if (properties.Feature.Parent is SPSite)
  {
    SPWeb web = ((SPSite) properties.Feature.Parent).RootWeb;
    switch (upgradeActionName)
    {
      case "AddFieldToContentType":
        string fieldId = parameters["FieldId"];
        string contentTypeId = parameters["ContentTypeId"];
        bool updateChilds = true;
        bool flag;
        updateChilds=bool.TryParse(parameters["PushDown"],out flag);
        AddFieldToContentType(web,contentTypeId,fieldId,updateChilds);
        break;
      default:
        break;
    }
  }
}


AddFieldToContentType method:
private void AddFieldToContentType(SPWeb web, string contentTypeId, string fieldId, bool updateChilds)
{
  SPContentType type = web.ContentTypes[new SPContentTypeId(contentTypeId)];
  type.FieldLinks.Add(new SPFieldLink(web.Fields[new Guid(fieldId)]));
  type.Update(updateChilds, updateChilds);
}


The last thing to do is to override the FeatureActivated method, this method is invoked when the feature is activated and it will make sure that the new Field also is added to the Content Type the first time the feature is activated:
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
  if (properties.Feature.Parent is SPSite)
  {
    SPWeb site = ((SPSite) properties.Feature.Parent).RootWeb;
    const string fieldId = "x-x-x-x-x";
    const string contentTypeId = "xxx";
    AddFieldToContentType(site, contentTypeId, fieldId, true);
  }
}


So to upgrade the feature run this script in PowerShell :
$versionFolder = "C:\temp\"
$solutionName = “x.wsp”
$solutionPath = $versionFolder + $solutionName
$siteUrl = "
http://.../"
$featureId = "{x-x-x-x-x}"

Update-SPSolution –Identity $solutionName –LiteralPath $solutionPath –GacDeployment
# wait for it to finish
$site = get-spsite $siteUrl
$feature = $site.Features | where {$_.Definition.Id -eq $featureId}

if($feature)
{
    $ex = $feature.Upgrade($true)
    Write-Host $ex #if anything went wrong this is printed
}

04/12/2012

SharePoint 2010 Document Sets

Document sets basics


Document sets are a part of SharePoint Document Management. Document Set enables grouping multiple documents, that support a single project or task, together into a single entity. A document set can in many ways be thought of as a folder, but a document set has some additional features:
  • Share the meta data
  • Can be versioned
  • Document sets use Content Types, Document set Content Types, you can create your own custom document set content type, a Document Set can therefor have all Content Types features (information policies, workflows and meta data)
  • Shares a common home page
Examples:

Test Document Set

A test Document Set can be created to group all test documents related to an application. Allowed Content Types in a test Document Set can be Test descriptions (step by step test steps), Test Reports (results of a test run through) and Test Plans (plans for how, who and when to run the tests).
The Test Document Set can contain a column named category, this category can always be set to "Test documentation" when documents are added to the document set. As an alternative the Document Set may contain only test documents connected to a specific release. The document set can then have another property named release version. This property can also be propagated down to all contained documents. The Test Document Set should have a description containing what documents should be placed in the set.

Design Document Set

A design Document Set can be used as a group of design documentation and supporting documents. The allowed Content Types would typically be Design Document Content Type and maybe a general Content Type used for all supporting documents. A periodic review workflow shall be associated with the document set to ensure that the documents are reviewed each year.

Department Budget Document Set:

A Document Set containing all budgets for 2012 for a specific department and sub departments.


How to create a Document Set Content Type

The document set feature must be activate: Site Settings > Site Collection Features:



Create a new document set: Site Settings > Galleries > Content Types > Create, the document set Content Types are displayed in the Document Set Content Types group:


When the new document set is created start by adding columns, click on Add from existing site columns or Add from new site column. Set the columns to required or not (click on the column after it is created):

Possible Document Set settings:
  • Restrict the Content Types allowed in the document set by setting the Allowed Content Types: the default settings are that only documents with Content Type document are allowed
  • Default Content: is there any content that always shall be added to the document set when created?
  • Consider if each file in a document set shall be prefixed by the name of the Document Set
  • Which column values for the Document Set should be automatically synchronised to all documents contained in the set? If a property is set for the document set this can be synchronised to all containing documents.
  • Consider what properties shall be shown on the welcome page: the welcome page is shown when opening the document set, a default welcome page is shown below
  • Customise the welcome page: you can change the text and image shown
Associate the new Document Set Content Type with a document library: Library > Library Settings > Add from existing Site Content Types. Choose Content Type Group and add it.

Now try to create a new document set. Go to the library, choose Documents > New Document > My Document Set:

Set name and properties of your new document set.

The new document set with the default welcome page will look like this:

 

Add a new document to the document set: Documents > New Document > Document, notice that the only Content Type allowed is Document:


The Document Set appears like this in the library:

SharePoint 2010 Content Types

Content Types basics


A content type is a reusable collection of meta data (columns), workflow, behaviour, and other settings for a category of items or documents in a list or document library. Content types enable you to manage the settings for a category of information in a centralised, reusable way. A content type defines the attributes of a list item, a document, a document set or a folder.

Example of a content type: Test description document content type:

Test description is a document containing step by step test steps that is used when testing an application.The test description Content Type is used for test description documents in a document library that contains system related documentation.
All test descriptions must be tagged with what system the test description tests and who is the owner of the document. These are properties associated with the test description Content Type.
All test descriptions has the same document headings, document header and document footer. It is therefor created a test description document template. This template is associated with the test description Content Type. When creating a new test description in the document library this template is automatically used.
A periodic review is associated with the test description, a review is forced on the document owner before every release of the system (released periodically). This is a workflow associated with the Content Type.
For traceability reasons auditing is also required, every time a test description is changed the id of the test description document is logged. This is an Auditing Policy associated with the Content Type.
The id associated with the Content Type is prefixed with TDD (Test Description Document), this is an example of a custom feature of this Content Type.

This can be specified for a content type:
  • Properties to associate with items of its type: columns of the content type, these are displayed when a new document is created and when the document is shown in the document list. The columns are also shown in the Document Information Panel in Office products. Columns can be reused.
  • Metadata to associate with items of its type: Metadata is information about a document that is used to categorise and classify your content. Metadata is associated with a content type as a column. A column can be mandatory to ensure that the meta data is provided.
  • Workflows that can be started from items of its type: e.g. periodic workflow review
  • Information management policies to associate with items of its type: 
    • auditing: logging when an event occurs
    • retention policies: define retention stages and an action that happens at the end of each stage, e.g. moving the item to the Recycle Bin, deleting an item or moving an item to another location
    • labels to ensure that physical copies of each document are properly identifiable
    • print restrictions, to ensure that sensitive employee-related documents are printed only on secure printers 
  • Document templates (for document content types)
  • Custom features: e.g. changing the id pattern
Document libraries and lists can contain multiple content types. SharePoint comes with a set of OOTB Content Types. Content types are organised into a hierarchy that lets one content type inherit its characteristics from another content type. Content types can be shared on different SharePoint sites.