none
Break item permission on itemadding RRS feed

  • Question

  • how can i break the inheritance of an item on item adding?

    ----------------------- Sharepoint Newbie

    Wednesday, May 29, 2013 1:12 AM

Answers

  • You can try this source code

    Take a look at this link

    http://blog.sharepointclick.com/2013/04/programatically-breaking-inheritance.html

    http://extendtheenterprise.com/2012/06/20/item-event-receiver-that-updates-permissions-using-itemadded-override/

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Collections.Specialized;
     
    namespace SurveyPermissionsMgr
    {
        class ThreeMonthSurveyPermissionsMgr : SPItemEventReceiver
        {
            public override void ItemAdded(SPItemEventProperties properties)
            {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(properties.WebUrl))
                        {
                            site.AllowUnsafeUpdates = true;
     
                            using (SPWeb web = site.OpenWeb())
                            {
                                web.AllowUnsafeUpdates = true;
     
                                SPListItem myItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID); //  GET LIST OBJECT FROM SPWEB, NOT EVENT PROPERTIES
     
                                myItem.BreakRoleInheritance(false);
     
                                this.DisableEventFiring();
                                myItem.Update();
     
                                SPMember member = web.SiteGroups["My Group"];
                                SPPrincipal principal = (SPPrincipal)member;
                                SPRoleDefinition roledefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                SPRoleAssignment myRoleAssignment = new SPRoleAssignment(principal);
                                myRoleAssignment.RoleDefinitionBindings.Add(roledefinition);
                                myItem.RoleAssignments.Add(myRoleAssignment);
     
                                string managerstring = myItem["Manager Account Id"].ToString();
                                SPUser manager = web.EnsureUser(managerstring);
     
                                myItem["Manager"] = manager;
     
                                SPPrincipal managerprincipal = (SPPrincipal)manager;
                                SPRoleDefinition managerroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                SPRoleAssignment ManagerRoleAssignment = new SPRoleAssignment(managerprincipal);
                                ManagerRoleAssignment.RoleDefinitionBindings.Add(managerroledefinition);
                                myItem.RoleAssignments.Add(ManagerRoleAssignment);
     
                                string associatestring = myItem["Author"].ToString();
                                int userid = Convert.ToInt32(associatestring.Substring(0, associatestring.IndexOf(";#")));
                                SPUser associate = web.AllUsers.GetByID(userid);
                                SPPrincipal associateprincipal = (SPPrincipal)associate;
                                SPRoleDefinition associateroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                SPRoleAssignment AssociateRoleAssignment = new SPRoleAssignment(associateprincipal);
                                AssociateRoleAssignment.RoleDefinitionBindings.Add(associateroledefinition);
                                myItem.RoleAssignments.Add(AssociateRoleAssignment);
     
                                myItem.Update();
                                this.EnableEventFiring();
                                web.AllowUnsafeUpdates = false;
                                site.AllowUnsafeUpdates = false;
     
                                //Handle messaging
                                StringDictionary dict = new StringDictionary();
                                dict.Add("to", manager.Email);
                                dict.Add("from", "us@you.com");
                                dict.Add("subject", "bla bla bla");
                                string msgbody = "bla bla bla";
                                SPUtility.SendEmail(web, dict, msgbody);
                            }
                        }
                    });
                }
            }
        }
    }



    Everything about SQL Server | Experience inside SQL Server -Mohammad Nizamuddin

    Wednesday, May 29, 2013 2:34 AM

All replies

  • You can try this source code

    Take a look at this link

    http://blog.sharepointclick.com/2013/04/programatically-breaking-inheritance.html

    http://extendtheenterprise.com/2012/06/20/item-event-receiver-that-updates-permissions-using-itemadded-override/

    using System;
    using System.Collections.Generic;
    using System.Text;
    using Microsoft.SharePoint;
    using Microsoft.SharePoint.Utilities;
    using System.Collections.Specialized;
     
    namespace SurveyPermissionsMgr
    {
        class ThreeMonthSurveyPermissionsMgr : SPItemEventReceiver
        {
            public override void ItemAdded(SPItemEventProperties properties)
            {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        using (SPSite site = new SPSite(properties.WebUrl))
                        {
                            site.AllowUnsafeUpdates = true;
     
                            using (SPWeb web = site.OpenWeb())
                            {
                                web.AllowUnsafeUpdates = true;
     
                                SPListItem myItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID); //  GET LIST OBJECT FROM SPWEB, NOT EVENT PROPERTIES
     
                                myItem.BreakRoleInheritance(false);
     
                                this.DisableEventFiring();
                                myItem.Update();
     
                                SPMember member = web.SiteGroups["My Group"];
                                SPPrincipal principal = (SPPrincipal)member;
                                SPRoleDefinition roledefinition = web.RoleDefinitions.GetByType(SPRoleType.Contributor);
                                SPRoleAssignment myRoleAssignment = new SPRoleAssignment(principal);
                                myRoleAssignment.RoleDefinitionBindings.Add(roledefinition);
                                myItem.RoleAssignments.Add(myRoleAssignment);
     
                                string managerstring = myItem["Manager Account Id"].ToString();
                                SPUser manager = web.EnsureUser(managerstring);
     
                                myItem["Manager"] = manager;
     
                                SPPrincipal managerprincipal = (SPPrincipal)manager;
                                SPRoleDefinition managerroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                SPRoleAssignment ManagerRoleAssignment = new SPRoleAssignment(managerprincipal);
                                ManagerRoleAssignment.RoleDefinitionBindings.Add(managerroledefinition);
                                myItem.RoleAssignments.Add(ManagerRoleAssignment);
     
                                string associatestring = myItem["Author"].ToString();
                                int userid = Convert.ToInt32(associatestring.Substring(0, associatestring.IndexOf(";#")));
                                SPUser associate = web.AllUsers.GetByID(userid);
                                SPPrincipal associateprincipal = (SPPrincipal)associate;
                                SPRoleDefinition associateroledefinition = web.RoleDefinitions.GetByType(SPRoleType.Reader);
                                SPRoleAssignment AssociateRoleAssignment = new SPRoleAssignment(associateprincipal);
                                AssociateRoleAssignment.RoleDefinitionBindings.Add(associateroledefinition);
                                myItem.RoleAssignments.Add(AssociateRoleAssignment);
     
                                myItem.Update();
                                this.EnableEventFiring();
                                web.AllowUnsafeUpdates = false;
                                site.AllowUnsafeUpdates = false;
     
                                //Handle messaging
                                StringDictionary dict = new StringDictionary();
                                dict.Add("to", manager.Email);
                                dict.Add("from", "us@you.com");
                                dict.Add("subject", "bla bla bla");
                                string msgbody = "bla bla bla";
                                SPUtility.SendEmail(web, dict, msgbody);
                            }
                        }
                    });
                }
            }
        }
    }



    Everything about SQL Server | Experience inside SQL Server -Mohammad Nizamuddin

    Wednesday, May 29, 2013 2:34 AM
  • Hi,

    Programatically It's only possible to break permissions on the list item object. So you should do that in ItemAdded event. In ItemAdding event the List item is not yet created and the ListItem property will be null.

    You can write this piece of code in ItemAdded for breaking the inheritance:

    SPListItem newItem = web.Lists[properties.ListId].GetItemById(properties.ListItem.ID); 
    newItem.BreakRoleInheritance(false);                               

    Please see following article for further reference:

    http://kiran-kakanur.blogspot.sg/2010/02/sharepoint-breakroleinheritance-and.html

    Hope It might help you


    Please don't forget to 'mark answer/propose answer' or 'vote as helpful' as appropriate.



    Wednesday, May 29, 2013 3:36 AM