Event receiver in SP 2013

hi,

below code explains how to apply permissions on an  item in a document library when an upload happens.using  a itemupdated event receiver in SP 2013 using VS 2012 :

1) it has removed all the role associations for the document . this includes entire group and  entire users.

2)it has also applied permissions on the  entire “everyone” users on the AD with  read permissions.

hope this helps someone.

using System;
using System.Web;
using System.Web.Configuration;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;
using Microsoft.SharePoint.Taxonomy;
using Microsoft.SharePoint.Administration;

namespace testt
{
    /// <summary>
    /// List Item Events
    /// </summary>
    public class tests1: SPItemEventReceiver
    {

        #region  variables

        #endregion  variables

        /// <summary>
        /// An item was updated.
        /// </summary>
        public override void ItemUpdated(SPItemEventProperties properties)
        {
            SPUser currUser = properties.Web.CurrentUser;
            SPFieldUserValue flduserValue = new SPFieldUserValue(properties.Web, currUser.ID, currUser.Name);

            try
            {
                string currentuserIdd = properties.CurrentUserId.ToString();

                SPSecurity.RunWithElevatedPrivileges(delegate()
                {

                    SPSite CurrentSite = new SPSite(properties.SiteId);
                    SPWeb CurrentWeb = CurrentSite.OpenWeb(properties.RelativeWebUrl);
                    SPList CurrentList = CurrentWeb.Lists[properties.ListId];
                    SPListItem CurrentListItem;

                    SPWeb myRootWeb = CurrentSite.RootWeb;

                    CurrentListItem = CurrentWeb.Lists[properties.ListId].GetItemById(properties.ListItem.ID); 

                    string _mCurrentUserID = string.Empty;

                   
                   
                 //   SPGroup EnPUsersGroupinBIF = CurrentWeb.SiteGroups[“EnPUsers”];

                    if (CurrentListItem != null)
                    {
                        {
                            try
                            {

                                {
                                    //classification column a single line of text data type
                                    selectedClassificationValue = CurrentListItem[CLASSIFICATION].ToString();
                                }
                            }
                            catch (Exception explnc)
                            {
                                throw;
                            }
                            if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue == “Confidential” && selectedClassificationValue != “–Select–“)
                            {
                                CurrentWeb.AllowUnsafeUpdates = true;

                                CurrentListItem.BreakRoleInheritance(false);

                                //Break the ITEM Inheritence and Remove the other default groups 2) get the taxonomy from the document library 3)                        //Break the Inheritence

 

              //SPUser mallusers = properties.Web.EnsureUser(“NT AUTHORITY\\authenticated users”); //all the enp users would be getting ReadOnly access to this document

                                                         

                                SPRoleAssignmentCollection SPRoleAssColn = CurrentListItem.RoleAssignments;
                                for (int i = SPRoleAssColn.Count – 1; i >= 0; i–)
                                {
                                    SPRoleAssignment roleAssignmentSingle = SPRoleAssColn[i];
                                    System.Type t = roleAssignmentSingle.Member.GetType();
                                    if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                        SPRoleAssColn.Remove(i);
                                }                                                       

                                                        SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                                        SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                                        mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                                        CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                                        this.EventFiringEnabled = false;

                                                        //below code is working on 4th-june-2014 for modified by 4-june 9:30 pm
                                                        CurrentListItem[“Author”] = currentuserIdd;
                                                        CurrentListItem.Update();

                                                        CurrentListItem[“Editor”] = currentuserIdd;
                                                                                                                CurrentListItem.Update();
                                                        

                                                        this.EventFiringEnabled = true;
                                                        CurrentWeb.AllowUnsafeUpdates = false;
                            }
                                                        else if (!string.IsNullOrEmpty(selectedClassificationValue) && selectedClassificationValue != “Confidential” && selectedClassificationValue != “–Select–” && selectedClassificationValue == “Public”)
                            {
                                try
                                {
                                    CurrentWeb.AllowUnsafeUpdates = true;
                                    CurrentListItem.BreakRoleInheritance(false)                               
                                                     ///

                                    SPRoleAssignmentCollection SPRoleAssColnPub = CurrentListItem.RoleAssignments;
                                    for (int i = SPRoleAssColnPub.Count – 1; i >= 0; i–)
                                    {
                                        SPRoleAssignment roleAssignmentSingle = SPRoleAssColnPub[i];
                                        System.Type t = roleAssignmentSingle.Member.GetType();
                                        if (t.Name == “SPGroup” || t.Name == “SPUser”)
                                            SPRoleAssColnPub.Remove(i);
                                    }

                                    SPUser allusers = CurrentWeb.EnsureUser(“NT AUTHORITY\\authenticated users”);
                                    SPRoleAssignment AllBIFRoleassignment = new SPRoleAssignment(allusers);
                                    SPRoleDefinition biffroledefi = CurrentWeb.RoleDefinitions.GetByType(SPRoleType.Reader);
                                    AllBIFRoleassignment.RoleDefinitionBindings.Add(biffroledefi);
                                    CurrentListItem.RoleAssignments.Add(AllBIFRoleassignment);

 

                                    SPRoleAssignment mroleAssignmentforCurrUser = new SPRoleAssignment(currUser);
                                    SPRoleDefinition mroleDefinitionforCurrUser = CurrentWeb.RoleDefinitions[“Contribute”];
                                    mroleAssignmentforCurrUser.RoleDefinitionBindings.Add(mroleDefinitionforCurrUser);
                                    CurrentListItem.RoleAssignments.Add(mroleAssignmentforCurrUser);

                                    this.EventFiringEnabled = false;

                                    CurrentListItem[“Author”] = currentuserIdd; 

                                    CurrentListItem.Update();
                                   
                                    CurrentListItem[“Editor”] = currentuserIdd; 
                                    CurrentListItem.Update();
                                    this.EventFiringEnabled = true;
                                    CurrentWeb.AllowUnsafeUpdates = false;
                                }
                                catch (Exception expforPublicdocGroup) { LogthisError(expforPublicdocGroup); }
                            }
                                      }
                    }
                });
            }
            catch (Exception expforAccessConfid)
            {
                LogthisError(expforAccessConfid);
            }
        }

              public override void ItemUpdating(SPItemEventProperties properties)
        {
            base.ItemUpdating(properties);
        }

        public void LogthisError(Exception Expfor)
        {
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                WebPartsLoggingService.LogError(“Malat”, Expfor.StackTrace);

            });
        }

        public object _mAccessRights { get; set; }
    }

 

}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s