JordanBlackler
asked on
Setting List Item Permissions in Sharepoint 2007?
Hi All -
I'm using C#
I'm trying to find a way to set permissions for each item.
thanks
I'm using C#
I'm trying to find a way to set permissions for each item.
thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
My code references list.Items[1] ... which would actually be the second item in the list. If you only have one item in your list, make sure you code is changed to list.Items[0]
If that does not resolve the error -- let me know which line the error is failing on.
Thanks
If that does not resolve the error -- let me know which line the error is failing on.
Thanks
ASKER
new error:
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. at Microsoft.SharePoint.Libra ry.SPReque stInternal Class.Upda teRoleAssi gnment(Str ing bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove)
at Microsoft.SharePoint.Libra ry.SPReque st.UpdateR oleAssignm ent(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove)
The security validation for this page is invalid. Click Back in your Web browser, refresh the page, and try your operation again. at Microsoft.SharePoint.Libra
at Microsoft.SharePoint.Libra
SPSite Site = new SPSite("http://****");
SPWeb web = Site.OpenWeb();
SPList list = web.Lists["myList"];
SPListItem item = list.Items[1]; // gets an item out of the list
SPRoleDefinition definition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPRoleAssignment assignment = new SPRoleAssignment(@"na\tester", "tester@test.com", null, null);
assignment.RoleDefinitionBindings.Add(definition);
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(false); // true if you want to copy permissions
item.RoleAssignments.Add(assignment);
item.Update();
right after the SPWeb web = Site.OpenWeb() line put the following:
web.AllowUnsafeUpdates = true;
ASKER
I still get that error, but added me to the list with Full Control.
When i click the back button when i get the erroe and run the code again, i don't get an error and it finally gives the user that i requested Full Control.
Any ideas?
here is my code.
When i click the back button when i get the erroe and run the code again, i don't get an error and it finally gives the user that i requested Full Control.
Any ideas?
here is my code.
protected void AddPermissions()
{
SPSite Site = new SPSite("http://****");
SPWeb web = Site.OpenWeb();
web.AllowUnsafeUpdates = true;
SPList list = web.Lists["myList"];
SPListItem item = list.GetItemById(Convert.ToInt32(lblUnique.Text));
SPRoleDefinition definition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
SPRoleAssignment assignment = new SPRoleAssignment(@"na\Tester", "Tester@test.com", null, null);
assignment.RoleDefinitionBindings.Add(definition);
if (!item.HasUniqueRoleAssignments)
item.BreakRoleInheritance(false); // true if you want to copy permissions
item.RoleAssignments.Add(assignment);
item.Update();
}
protected void btnSecondSave_Click(object sender, EventArgs e)
{
AddPermissions();
SPSite site = SPControl.GetContextSite(Context);
site.CatchAccessDeniedException = false;
SPWeb subSite = site.AllWebs["mySite"];
SPListCollection allSiteLists = subSite.Lists;
SPList list = subSite.Lists["myList"];
SPListItemCollection listItems = list.Items;
SPListItem newItem = list.GetItemById(Convert.ToInt32(lblUnique.Text));
subSite.AllowUnsafeUpdates = true;
newItem["Status"] = lblStatus.Text;
newItem["StartDate"] = txtStartDate.Text;
//
//
newItem.Update();
}
Add the web.AllowUnsafeUpdates = true; after you break the inheritance. I forgot to mention this -- the BreakRoleInheritance method reverts the AllowUnsafeUpdates back to false.
ASKER
yup, that did it. Thanks very much.
ASKER
Value does not fall within the expected range. at Microsoft.SharePoint.SPRol
at Microsoft.SharePoint.SPRol
at _Default.Button1_Click(Obj
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.WebControls.
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.RaisePo
at System.Web.UI.Page.Process
Open in new window