Solved

Setting List Item Permissions in Sharepoint 2007?

Posted on 2007-11-20
8
1,897 Views
Last Modified: 2009-12-16
Hi All -
I'm using C#

I'm trying to find a way to set permissions for each item.

thanks
0
Comment
Question by:JordanBlackler
  • 4
  • 4
8 Comments
 
LVL 15

Accepted Solution

by:
liebrand earned 500 total points
ID: 20320365
This is assuming you already have the item loaded into an SPListItem object.


SPListItem item = list.Items[1]; // gets an item out of the list

SPRoleDefinition definition = web.RoleDefinitions.GetByType(SPRoleType.Administrator);
 

SPRoleAssignment assignment = new SPRoleAssignment(@"domain\user", "email@company.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();

Open in new window

0
 

Author Comment

by:JordanBlackler
ID: 20320602
I get this error:
Value does not fall within the expected range.   at Microsoft.SharePoint.SPRoleAssignmentCollection.UpdateAssignment(Int32 principalId, SPRoleDefinitionBindingCollection bindings, Boolean addOnly)
   at Microsoft.SharePoint.SPRoleAssignmentCollection.Add(SPRoleAssignment roleAssignment)
   at _Default.Button1_Click(Object sender, EventArgs e)
   at System.Web.UI.WebControls.Button.OnClick(EventArgs e)
   at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)
   at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
   at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


        SPWeb web = SPControl.GetContextWeb(Context);

        SPSite site = SPControl.GetContextSite(Context);

        //site.CatchAccessDeniedException = false;

        SPWeb subSite = site.AllWebs["mySite"];

        SPListCollection allSiteLists = subSite.Lists;

        SPList list = subSite.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\test", "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();

Open in new window

0
 
LVL 15

Expert Comment

by:liebrand
ID: 20320655
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
0
 

Author Comment

by:JordanBlackler
ID: 20320751
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.Library.SPRequestInternalClass.UpdateRoleAssignment(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove)
   at Microsoft.SharePoint.Library.SPRequest.UpdateRoleAssignment(String bstrUrl, Guid& pguidScopeId, Int32 lPrincipalID, Object& pvarArrIdRolesToAdd, Object& pvarArrIdRolesToRemove)
        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();

Open in new window

0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 15

Expert Comment

by:liebrand
ID: 20320853
right after the SPWeb web = Site.OpenWeb() line put the following:
web.AllowUnsafeUpdates = true;

Open in new window

0
 

Author Comment

by:JordanBlackler
ID: 20321368
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.
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();

}

Open in new window

0
 
LVL 15

Expert Comment

by:liebrand
ID: 20321400
Add the web.AllowUnsafeUpdates = true; after you break the inheritance.  I forgot to mention this -- the BreakRoleInheritance method reverts the AllowUnsafeUpdates back to false.
0
 

Author Comment

by:JordanBlackler
ID: 20321612
yup, that did it. Thanks very much.
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Summary In SharePoint 2010 it is easy to create custom color themes to jazz up a site. Theme colors can also be created in PowerPoint 2010 with a few clicks. But how do the chosen colors actually look in the SharePoint site? The attached PowerPoint…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now