Tuesday, March 18, 2008

Assigned item level permission (Moss 2007)

Currently working on a new project which requires WF auto assign the permission at document level once this document finish with all the Approvals.

Console App sample Code for you to test:

static void Main(string[] args)
{
string strResult = "";
string strFileID = "1";
try
{
SPSite WebApp = new SPSite("http://security/");
SPWeb Site = WebApp.OpenWeb();
SPList list = Site.Lists["Documents for Review"];
SPQuery newSPQuery = new SPQuery();
newSPQuery.Query = "" + strFileID + "";

SPListItemCollection listItemCol = list.GetItems(newSPQuery);
if (listItemCol.Count > 0)
{
foreach (SPListItem item in listItemCol)
{
SPRoleDefinition RoleDefinition = Site.RoleDefinitions.GetByType(SPRoleType.Reader);
SPRoleAssignment RoleAssignment = new SPRoleAssignment("us\\MOSSUser, "rajasudhakar@shotmail.com", "Raja Dandu", "Test via Code notes");
RoleAssignment.RoleDefinitionBindings.Add(RoleDefinition);

if (!item.HasUniqueRoleAssignments)
{
item.BreakRoleInheritance(true);
}
item.RoleAssignments.Add(RoleAssignment);
item.Update();
}
}
}
catch (Exception ex)
{
strResult += "Permission not set, reason: " + ex.Message;
}


Related links:
http://msdn2.microsoft.com/en-us/library/ms414036.aspx
http://msdn2.microsoft.com/en-us/library/ms414400.aspx
http://office.microsoft.com/en-us/sharepointtechnology/CH100649861033.aspx

No comments: