Monthly Archives: August 2014

Query Managed Metadata Column in SharePoint List / Library

Query

Following my previous article to Populate Managed Metadata Column, In this article I would like to show you on How to Query Managed Metadata column in SharePoint List / Library.

If you look at the msdn article, the query would look like below:

<Where>
 <In>
  <FieldRef LookupId='TRUE' Name='FieldName' />
  <Values>
   <Value Type='Integer'>14</Value>
   <Value Type='Integer'>15</Value>
  </Values>
 </In>
</Where>

 

The question is where do we get that Values in the query ?

To get those values, I’ve created a SharePoint console application.

static void Main(string[] args)
{
   using(SPSite site = new SPSite("http://yoursiteurl"))
   {
      using (SPWeb web = site.OpenWeb())
      {
         SPList list = web.Lists.TryGetList("MyList");
         TaxonomyField audienceType = (TaxonomyField)site.RootWeb.Fields.GetField("MyManagedColumn");
         TaxonomySession taxonomySession = new TaxonomySession(site);
         TermStore termStore = taxonomySession.TermStores[audienceType.SspId];
         TermSet termSet = termStore.GetTermSet(audienceType.TermSetId);

         TermCollection termColl = termSet.GetAllTerms();
         foreach (Term eachTerm in termColl)
         {
            int[] wssIds = TaxonomyField.GetWssIdsOfTerm(site, termStore.Id, termSet.Id, eachTerm.Id, false, 500);
            if(wssIds.Length > 0)
               Console.WriteLine(string.Format("Term: {0}, WssId: {1}", eachTerm.Name.Trim(), wssIds[0].ToString()));
         }
      }
   }
   Console.ReadLine();
}

 
Those Wss Id are that you need to put as the Value.
 
References

 
 
 
 
 

Advertisements

Configure Information Management Policy programmatically

Following my previous article to set Expiry column using Reusable Workflow, I would also like to Configure Information Management Policy at Content type programmatically on Feature Activated.

What I could do first is to manually configure Information Management Policy at Content type to look like below.

RetentionPolicy1

 

Then, I’ve created simple SharePoint console application to retrieve the Information Management Policy custom data. It looks like below:

using System;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using Microsoft.Office.RecordsManagement.InformationPolicy;

namespace GetRetentionPolicyCustomData
{
    class Program
    {
        static void Main(string[] args)
        {
            using (SPWeb web = new SPSite("http://yourSiteURL").OpenWeb())
            {
                // Get Policy Custom Data
                string contentTypeName = "YourContentTypeName";
                SPContentType contentType = web.ContentTypes.Cast<SPContentType>().Where(cty => cty.Name.Trim().ToLower() == contentTypeName.ToLower()).FirstOrDefault();

                if (contentType != null)
                {
                    Policy policy = Policy.GetPolicy(contentType);
                    if (policy != null)
                    {
                        foreach (PolicyItem eachPolicy in policy.Items)
                        {
                            string customdata = eachPolicy.CustomData;
                            Console.WriteLine("Policy " + eachPolicy.Name + ": " + customdata);
                        }
                    }
                }
            }
        }
    }
}

 

And you will get something like below in Information Management Policy Custom Data variable.

 

<Schedules nextStageId="3">
 <Schedule type="Default">
  <stages>
   <data stageId="1">
    <formula id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn">
     <number>0</number>
     <property>NearExpiry</property>
     <propertyId>15686f6f-8d25-4eba-be79-7792663f4675</propertyId>
     <period>days</period>
    </formula>
    <action type="workflow" id="f5c7ade6-1a7e-4231-bce8-81041b1b7fcd" />
   </data>
   <data stageId="2">
    <formula id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn">
     <number>7</number>
     <property>Expiration</property>
     <propertyId>d2681865-a6d3-4d5d-850b-cd2a1be22b31</propertyId>
     <period>years</period>
    </formula>
    <action type="action" id="Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin" />
   </data>
  </stages>
 </Schedule>
</Schedules>

 

Now, Delete the Information Management Policy that you’ve configured manually on the Content Type and let implements it programmatically.
In Site Collection scope feature event receiver:

 

using Microsoft.Office.RecordsManagement.InformationPolicy;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Workflow;
using System;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace InformationManagementPolicy
{
    /// <summary>
    /// This class handles events raised during feature activation, deactivation, installation, uninstallation, and upgrade.
    /// </summary>
    /// <remarks>
    /// The GUID attached to this class may be used during packaging and should not be modified.
    /// </remarks>

    [Guid("92cf61e2-0ead-4a9d-b22d-4ff8969e1d05")]
    public class RetentionPolicyEventReceiver : SPFeatureReceiver
    {
        private const string LIST_WORKFLOWTASKS = "Workflow Tasks";
        private const string LIST_WORKFLOWHISTORY = "Workflow History";
        private const string COLUMN_EXPIRATION = "Expiration";
        private const string COLUMN_NEAREXPIRY = "NearExpiry";
        private const string WORKFLOW_SENDNOTIFICATION = "Send Expiry Notification Workflow";

        /// <summary>
        /// Event handler when feature activated
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPWeb web = new SPSite(((SPSite)properties.Feature.Parent).ID).OpenWeb())
                    {
                        string contentTypeName = "YourContentTypeName";
                        SPContentType contentType = web.ContentTypes.Cast<SPContentType>().Where(cty => cty.Name.Trim().ToLower() == contentTypeName.Trim().ToLower()).FirstOrDefault();
                        if (contentType != null)
                        {
                            // Attach Retention Policies to Content Type
                            CreateRetentionPolicyInContentType(web, contentType, COLUMN_EXPIRATION, COLUMN_NEAREXPIRY);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                // exception "Issue found when CNP.CWE.CDA.Workflows feature activated."
            }
        }

        /// <summary>
        /// Event handler when feature deactivating
        /// </summary>
        /// <param name="properties"></param>
        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPSecurity.RunWithElevatedPrivileges(delegate
                {
                    using (SPWeb web = new SPSite(((SPSite)properties.Feature.Parent).ID).OpenWeb())
                    {
                        string contentTypeName = "YourContentTypeName";
                        SPContentType contentType = web.ContentTypes.Cast<SPContentType>().Where(cty => cty.Name.Trim().ToLower() == contentTypeName.Trim().ToLower()).FirstOrDefault();
                        if (contentType != null)
                        {
                            // Delete Retention Policies to Content Type
                            if (Policy.GetPolicy(contentType) != null)
                                Policy.DeletePolicy(contentType);
                        }
                    }
                });
            }
            catch (Exception ex)
            {
                // exception "Issue found when CNP.CWE.CDA.Workflows feature deactivating."
            }
        }

        // 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)
        //{
        //}

        #region Private methods

        /// <summary>
        /// Method to attach retention policy to specified content type
        /// </summary>
        /// <param name="web">current web</param>
        /// <param name="contentType">content type</param>
        /// <param name="expirationFieldName">expiration field name</param>
        /// <param name="nearExpiryFieldName">near expiry field name</param>
        private void CreateRetentionPolicyInContentType(SPWeb web, SPContentType contentType, string expirationFieldName, string nearExpiryFieldName)
        {
            try
            {
                SPField expiration = contentType.Fields.Cast<SPField>().Where(fld => fld.InternalName == expirationFieldName).FirstOrDefault();
                SPField nearExpiry = contentType.Fields.Cast<SPField>().Where(fld => fld.InternalName == nearExpiryFieldName).FirstOrDefault();
                SPWorkflowAssociation workflowAssoc = contentType.WorkflowAssociations.Cast<SPWorkflowAssociation>().Where(assoc => assoc.Name == WORKFLOW_SENDNOTIFICATION).FirstOrDefault();

                if (expiration != null && nearExpiry != null && workflowAssoc != null)
                {
                    if (Policy.GetPolicy(contentType) == null)
                        Policy.CreatePolicy(contentType, null);
                    Policy policy = Policy.GetPolicy(contentType);

                    string policyCustomData = GeneratePolicyCustomData(expiration, nearExpiry, workflowAssoc.ParentAssociationId.ToString());
                    policy.Items.Add("Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration", policyCustomData);
                    contentType.Update();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        /// <summary>
        /// Method to generate Policy Custom Data
        /// </summary>
        /// <param name="expiration">expiration column</param>
        /// <param name="nearExpiry">near expiry column</param>
        /// <param name="parentAssociationId">send notification parent workflow association</param>
        /// <returns></returns>
        private string GeneratePolicyCustomData(SPField expiration, SPField nearExpiry, string parentAssociationId)
        {
            StringBuilder sb = new StringBuilder();
            try
            {
                sb.AppendLine("<Schedules nextStageId='3'>");
                sb.AppendLine("<Schedule type='Default'>");
                sb.AppendLine("<stages>");

                // Send Expiry Notification when Today = Near Expiry + 0 days
                sb.AppendLine("<data stageId='1'>");
                sb.AppendLine("<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>");
                sb.AppendLine("<number>0</number>");
                sb.AppendFormat("<property>{0}</property>", nearExpiry.InternalName);
                sb.AppendFormat("<propertyId>{0}</propertyId>", nearExpiry.Id.ToString());
                sb.AppendLine("<period>days</period>");
                sb.AppendLine("</formula>");
                sb.AppendFormat("<action type='workflow' id='{0}' />", parentAssociationId);
                sb.AppendLine("</data>");

                // Delete the item after Expiration + 7 years
                sb.AppendLine("<data stageId='2'>");
                sb.AppendLine("<formula id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Formula.BuiltIn'>");
                sb.AppendLine("<number>7</number>");
                sb.AppendFormat("<property>{0}</property>", expiration.InternalName);
                sb.AppendFormat("<propertyId>{0}</propertyId>", expiration.Id.ToString());
                sb.AppendLine("<period>years</period>");
                sb.AppendLine("</formula>");
                sb.AppendLine("<action type='action' id='Microsoft.Office.RecordsManagement.PolicyFeatures.Expiration.Action.MoveToRecycleBin' />");
                sb.AppendLine("</data>");

                sb.AppendLine("</stages>");
                sb.AppendLine("</Schedule>");
                sb.AppendLine("</Schedules>");
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return sb.ToString();
        }
        #endregion
    }
}

Import a SharePoint Designer Reusable Workflow into Visual Studio

Everyone googling for Import SharePoint Designer Reusable Workflow into Visual Studio has definitely covered this msdn article. I also have followed the article thoroughly for one my project task this week and found there is something missing at the last step of the article. Do you know what it is? Let me tell you the answer later on =)

So, let’s go start the implementation …

PreRequisites

1. Site Column “Expiration” as Date Time field
2. Site Content Type “MyContentType” as Custom List content type
ReusableWorkflow2
3. Custom List “MyList” based on “MyContentType”
ReusableWorkflow1

 

Step 1: Create Reusable Workflow in SharePoint Designer

I don’t want to go to much detail about this step, cause I found an excellent technet video from Asif Rehmani about creating reusable workflow in SharePoint Designer. Follow that video and you should be good. My reusable workflow looks like below.
ReusableWorkflow3
And after I publish it, I could see the workflow is available on MyContentType. Not in This List, Item or Folder content type.
ReusableWorkflow4

 

Step 2: Save Reusable Workflow as Template

 

In this step, we’re going to Save it As Template and it would be available as WSP file in Site Asset Library. Download the WSP file to your local folder. Note: Do NOT Publish Globally, since you WILL NOT be able to Save As Template afterwards.
ReusableWorkflow5
ReusableWorkflow6

 

Step 3: Import the WSP file into Visual Studio

1. In Visual Studio, on the menu bar, choose File > New > Project.

2. In the New Project dialog box, expand the SharePoint node under either Visual C# and then choose the 2010 node.

3. In the Templates pane, choose the Import Reusable SharePoint 2010 Workflow template, leave the name of the project as MyWorkflowImportProject, and then choose the OK button. The SharePoint Customization Wizard appears.

4. On the Specify the site and security level for debugging page, enter the URL for the second SharePoint site: http://<site url>/.

5. In the What is the trust level for this SharePoint solution? section, choose the Deploy as a farm solution option button, and then choose the Next button.

6. In the Specify the new project source page, browse to the location on the system where you previously saved the .wsp file, open the file, and then choose the Next button. Then, Choose the Finish button to import all available items in the .wsp file. This displays a list of reusable workflows available for importing.

7. In the Select items to import box, choose the SPD Task Workflow workflow, and then choose the Finish button.

After the import operation is finished, a project named MyWorkflowImportProject is created containing a workflow named MyReusableWorkflowFT. In this folder is the workflow’s definition file Elements.xml and the workflow designer file (.xoml). The designer contains two files: the rules file (.rules) and the code-behind .cs file.

8. In Solution Explorer, Delete the Other Imported Files folder.

9. In the Elements.xml file, Delete InstantiationURL=”_layouts/IniErkflIP.sspx” and Update Name=”My Reusable Workflow”.

10. Ensure MyReusableWorkflowFT module included in your Feature, Update Feature Title and Scope to Site.

11. Build and Deploy the solution. Do NOT forget to delete your SharePoint Designer Publishing Workflow that you created on step 1 so you dont get mix up.

 

Step 4: Associate the Workflow

So, I go to MyList Settings > Workflow Settings > Add a Workflow to associate My Reusable Workflow to the list. Then, I found the issue …

ReusableWorkflow7

 

The Problem and Missing Piece

I do NOT want this and I want My Reusable Workflow to be available on MyContentType only. After googling for sometime, I still can not find any clues … So, I have a look and follow on the msdn article again. Then, I started to see there is AssociationCategories element in Elements.xml under  MyReusableWorkflowFT. This is the Ahha moment …

Based on msdn article, the missing piece is to specify the AssociationCategories value whether it is General, List or ContentType. I found Mano Mangaldas blog that provide more example on AssociationCategories value variations.

My updated Elements.xml would look like below:

ReusableWorkflow8

 

 

Resources

 

 

%d bloggers like this: