Set file level permissions in SharePoint using Workflow Foundation

By Geert van der Cruijsen at February 18, 2009 15:23
Filed Under: SharePoint

Hello Everyone, 

On my current project we are building a customer facing SharePoint application which has to store files. These files are uploaded with biztalk so we have to set the permissions manually so only the user who the file belongs to has rights to open te file.

We are storing the username of the owner of the file in the metadata of the sharepoint item and we are going to use this to set the permissions after the file is uploaded to sharepoint by Biztalk.

After adding the file we've set up a SharePoint Workflow to move the files to a specific location and we've come up with the idea to also change the permissions in this workflow. How we did this? here's the code to change the permissions of a SharePoint SPItem in the workflow:

   1:  workflowProperties.Item.BreakRoleInheritance(false);
   2:   
   3:  workflowProperties.Item.File.MoveTo(currentFolder + "/" + workflowProperties.Item.File.Name);
   4:   
   5:  SPRoleDefinitionCollection rolecollection = web.RoleDefinitions;
   6:  SPRoleAssignmentCollection roleAssignments = workflowProperties.Item.RoleAssignments;
   7:   
   8:  SPUser user = web.EnsureUser("aspnetsqlmembershipprovider:"+workflowProperties.Item.Properties["Username"].ToString());
   9:  SPRoleAssignment roleAssignment = new SPRoleAssignment(user as SPPrincipal);
  10:  SPRoleDefinitionBindingCollection roleDefBindings = roleAssignment.RoleDefinitionBindings;
  11:  roleDefBindings.Add(rolecollection.GetByType(SPRoleType.Reader));
  12:  roleAssignments.Add(roleAssignment);

 

We are using an aspnetsqlmembershipprovider to store the external users so thats why we add this to the sharepoint username.

Geert van der Cruijsen

Comments

9/1/2010 5:15:52 PM #

Been searching on how to set file level permissions in SharePoint using Workflow Foundation. Thanks I found this blog. Smile

Buy Panic Away

9/2/2010 4:41:10 PM #

Thank you for sharing the code on how to change the permissions in the workflow. It was good to be updated by adding the file set up on the sharepoint workflow. It was a nice post.

chicago carpet cleaning

9/6/2010 5:30:32 PM #

This past weekend the niece of Dr. Martin Luther King Jr. took the stage and tried to sell this nation a payday loan as the payment solution on that check her uncle spoke of 47 years ago.

reputable payday loan

Add comment



biuquote
Loading