Just another WordPress.com site

Archive for July, 2012

How to store custom class object in SharePoint property bag?

What is Property Bag?

SharePoint property bag is a place to store metadata or custom property value to different SharePoint objects. Property bag is implemented as a Key-Value pair hash-table.

Why to Use SharePoint Property Bag?

Generally developer use configuration file to store configurable properties like ConnectionString, some FilePath, some other custom setting. Since config file is allowed to store values as Key-Value pair, how property bag is useful to store information?


<configuration>
  <appSettings>
    <add key="WebAppLevelKey" value="MyValue1" />
    <add key="SiteLevelKey" value="MyValue2" />
    <add key="WebLevelKey" value="MyValue3" />
  </appSettings>
</configuration>

In SharePoint config file scope to web-application level so

  • We need to maintain different key-value for different scope because any value in config is same for all SPSite and SPWeb inside the web-application.
  • Config value is same for all users.
  • We should know the value at compile time. Information that is change or decide at run-time can’t be store here.

Levels

Property bag is available to SharePoint objects from SPFarm scope to SPList. Following SharePoint objects provide property bag: –

  • SPFarm
  • SPWebApplication
  • SPSite
  • SPWeb
  • SPList

UI to manage Property Bag

SharePoint doesn’t provide any interface inside the web to manage property bag. Although SharePoint designer provides a interface to manage property bag.

When you open a site in SharePoint Designer 2010, in the ribbon, all the way over to the right, you’ll see a button for Site Options

SiteOptions

By clicking on it,  a new window is opened. The first tab labeled Parameters. This parameters window is nothing else but a Property Bag window.

SiteOptionsWindow

From this window, you can add/modify/delete the property bag.

Access Property Bag through code

Most of the time property bag is useful to store information decide at run-time. As mention above different level of SharePoint object provides property bag to store data. To manage property bag through code, SharePoint API class exposes.

// Use this RunWithElevatedPrivileges to
// avoid error if user doesn't have permission to update.
SPSecurity.RunWithElevatedPrivileges(delegate()
 {
   try
    {
    using (SPSite RootSite = new SPSite(URL))
     {
      using (SPWeb rootWeb = RootSite.OpenWeb())
       {
       try
        {
         rootWeb.AllowUnsafeUpdates = true;
         // Get property value against MyKey key from Property bag
         if (rootWeb.AllProperties.ContainsKey("MyKey"))
          {
           string myValue = rootWeb.AllProperties["MyKey"].ToString();
          }
          // Set siteID in the Property bag
          rootWeb.Properties["siteID"] = siteID;
          rootWeb.Properties.Update();
          rootWeb.AllowUnsafeUpdates = false;
         }
         catch (Exception ex)
         {
           //Handle Exception
         }
        }
       }
      }
      catch(Exception ex)
      {
      }
   }
);

Store Custom Class Object INTO Property Bag

SharePoint property bag is allow to store any type of data that can be serialize. So to store custom class object, custom class should be mark as Serializable.


[System.Serializable]

public class CustomClass{

public string Name{

get;

set;

}

}

 

Advertisements

How to programmatically start sharepoint workflow

SharePoint workflow is great way to automate the business process.  SharePoint workflow can be configured to start automatic or manually. But sometimes our requirement lies in between two default options i.e. we want to start workflow automatically if and only if certain condition met e.g.  workflow initiation depends on certain business logic or depends on some field value entered by user.  If condition met we need to start workflow otherwise not. In this scenario, SharePoint default options ( Automatic OR Manual) to start workflow do not work and developer requires to start workflow at run time using code.

To start a workflow at run-time  we need two important values: –

  1. Workflow Association Reference Id OR Workflow association name: – Workflow association id that we want to start. OR workflow association name can also use to identify it.
  2. Workflow Initiation Data: – Its optional but most of the workflow requires some initiation data. Initiation data that requires by workflow to take decision. To know more about the initiation data and how to pass it, please refer http://wp.me/p1nAw1-m

To start any workflow, we requires following objects: –

  1. WorkflowManager: – With each SPSite object a SPWorkflowManager object is associated , enabling you to centrally control the workflow templates and instances across the site collection.  We can get WorkflowManager instace by SPSite instace.
    E.g.

     WorkflowManager = SPContext.Current.Web.Site.WorkflowManager;
    
  2.  SPWorkflowAssociation: – Workflow association is a binding between list/library and workflow template.  When a workflow is added to SharePoint list/library an workflow association is crated. We can get the workflow association by list/library where workflow is associated.
  3. E.g.
      SPWorkflowAssociationCollection associationColl = ParentList.WorkflowAssociations;
      foreach (SPWorkflowAssociation association in associationCollection) {
        // We can find the association by BaseId or by workflow association name.
        // if (String.Compare(objWorkflowAssociation.BaseId.ToString("B"), {"Workflow_GUID"}, true) == 0)
       // you can identify the workflow association by association name, entered during association creation.
      if (association.Name == wfName)        {
        association.AutoStartChange = true;
        association.AutoStartCreate = false;
        // We can change the
        association.AssociationData = string.Empty;
        // We can use other overload StartWorkflow methods.
       spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
      }
    }
    

So complete code to start workflow at runtime is

public void StartWorkflow(SPListItem listItem, SPSite spSite, string wfName)  {
    SPList parentList = listItem.ParentList;
    SPWorkflowAssociationCollection associationCollection = parentList.WorkflowAssociations;
    foreach (SPWorkflowAssociation association in associationCollection)  {
      if (association.Name == wfName){
       association.AutoStartChange = true;
       association.AutoStartCreate = false;
       association.AssociationData = string.Empty;
       spSite.WorkflowManager.StartWorkflow(listItem, association,  association.AssociationData);
       }
     }
}