Programmatically starting a workflow under specific User context

In SharePoint there are many scenarios we have to start workflow programmatically. This is very easy to achieve and there are plenty of blogs and references you will find which will show you how to start workflow programmatically.

There are few cases where you may want to start workflow under specific User context. To achieve that you simply have pass the user token when creating SPSite object.

In the following example we are going to start the workflow from an event receiver when someone adds a new document. We will use Author field to get the Author of the workflow and then pass that user token to the SPSite oject and then start the workflow programmatically.
public override void ItemAdded(SPItemEventPropertiesproperties)
{
SPListItem itemAdded = properties.ListItem;
// Get the Created By field value
String authorUser = itemAdded["Author"].ToString();
// Get the ID of the user.
int userID = Convert.ToInt32(authorUser.Substring(0, authorUser.IndexOf(";#")));
SPUser Author = properties.Web.ParentWeb.AllUsers.GetByID(userID);
SPUserToken authorToken = Author.UserToken;
// Open the SPSite again by passing the user context that we want our workflow to run under
using (SPSite site = newSPSite(properties.Web.Url, authorToken))
using (SPWeb web = site.OpenWeb())
{
SPList list = web.Lists["DOCUMENT LIBRARY"];
SPListItem lstItem = list.GetItemById(itemAdded.ID);
SPWorkflowManager manager = site.WorkflowManager;
SPWorkflowAssociationCollectionassoCollection = lstItem.ContentType.WorkflowAssociations;
// I am assuming that there is only one workflow in workflow association collection
// If you have more workflows then simply use If condition to start specific workflow.
foreach (SPWorkflowAssociationasso in assoCollection)
{
manager.StartWorkflow(lstItem, asso, asso.AssociationData.);
}
}
}

SharePoint Designer 2010 Workflow and Comment Out or Disable Workflow Actions

If you are designing a complex or long workflow using SharePoint Designer 2010 and for some reason during debugging process you may want to comment out certain actions or disable certain actions. I could not find any out of the box solution to this. I am not a big fan of SPD workflow but if I write a workflow in SPD I usually put IF condition around the actions that I want to comment out and if I want to run those actions my condition will be

If 1 equals 1

and if I dont want them to run then my condition will be

If 1 equals 2

Simple.!!

SharePoint Designer 2010 Workflow and Impersonation

This has already been posted couple of time already bust still posting this with little more details around using Impersonation SharePoint Designer.

Starting SharePoint Designer 2010 MS introduced this new step called “Impersonation Step” where any activity used inside this step will run under the workflow author (person who last edited the workflow in designer). Mind you this does not mean the person who started the workflow.

To use Impersonation step you simply click below your current step and from the ribbon click Impersonation step.

SPD.png

Once you add this step you will see a new step added to your workflow like shown below.

SPD1.png

Write value to a Target Audience Field Programmatically

Recently I came across an interesting question where user wanted to set the Target Audience Field value programmatically.

When setting this value through SharePoint UI you can either use your Audience which you have created using CA or you can use your SharePoint group.

So if you want to set Audience field value with the SharePoint Group using code it is very simple.​

In the following example I have enabled a list to have audience targetting. Doing this will add a field “Target Audience” to the list. Using the code I am updating the field value.

        private void setAudienceField()
{
SPWeb web = SPContext.Current.Web;;
SPList list = SiteWeb.Lists["YOUR LIST"];
web.AllowUnsafeUpdates = true;

SPGroup group = web.SiteGroups["YOUR SHAREPOINT GROUP NAME"];

SPListItem lstItem = list.Items.Add();

// set your list field values

............
// set Audience field
lstItem[lstItem.Fields["Target Audiences"].InternalName] = group.LoginName;

lstITem.Update();
web.AllowUnsafeUpdates = false;
}

If you want to update the audience field value with the audience name then the code is little different

        private void setAudienceField()
        {
                SPSite site = SPContext.Current.Site;
                SPWeb web = SPContext.Current.Web;
               SPList list = SiteWeb.Lists["YOUR LIST"];
               web.AllowUnsafeUpdates = true;

               Audience myAudience;

               SPServiceContext context = SPServiceContext.GetContext(site);

               AudienceManager audienceMgr = new AudienceManager(context);

               myAudience = audienceMgr.GetAudience("YOUR AUDIENCE NAME");

               SPListItem lstItem = list.Items.Add();

                 // set your list field values

                ............
                // set Audience field
               lstItem[lstItem.Fields["Target Audiences"].InternalName] = myAudience.AudienceID.ToString();
           
                itemToAdd.Update();
                web.AllowUnsafeUpdates = false;
        }

Anonymous Blog site on Office 365

Hello Everyone

So finally I have decided to move my blog to Office 365 and first thing I would like to do is to thank Martin Hatch. When I started playing with my blog site I realized that there is no way I can turn the anonymous permission on for this site. Luckily I found this article by Martin Hatch which shows how you can turn anonymous permission on for your blog.

So if you want to host your blog on Office 365 and you are running into issues with anonymous permission setting then I would highly recommend you to check this article out.

Over the next few days I will be moving all my old blog posts and then continue to blog about some interesting things I learned with SharePoint.

Happy SharePointing.