none
Event Handler to check the permissions and delete the item

    Question

  • Hi

    Can anyone help me with writing an eventhandler where it checks the permissions of the logged in user and executes the delete item action based on permissions.

    if the logged user is administrator then he can delete the item else the user must recieve an error message.

    i am new to the programming for sharepoint.

    thanks

     

     

    Monday, April 12, 2010 2:03 PM

Answers

  • Try this......................

    public override void ItemDeleting(SPItemEventProperties properties)
    {
         base.ItemAdded(properties);

    try
    {

        SPWeb web = _properties.OpenWeb();

          SPUser CurrentUser=web.SiteUsers[_properties.UserLoginName];

               if (!CurrentUser.IsSiteAdmin)  //Check whether user is administrator {               

                    _properties.Cancel = true;

                    _properties.ErrorMessage = "You are not authorized to delete the Item. Contact Administrator of site.";

                   
             }

    }
    catch (Exception ex)
    {

         // Log exceptions.........
    }

    }


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    Monday, April 12, 2010 2:37 PM
  • Hi,

    For creating the event handler and trapping the ItemDeleting event, try these links:

    http://www.wrox.com/WileyCDA/Section/Programming-Event-Handling-in-Windows-SharePoint-Services.id-306329.html

    http://msdn.microsoft.com/en-us/library/ms453149.aspx

    And, for checking permissions, you can write some code like given below:

    SPUser user = currentTeamSite.AllUsers.GetByID(properties.CurrentUserId);

    if (user.IsSiteAdmin)
    {
       //Do something 
    }
    else
    {
        properties.Cancel = true;
        properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";

    }

    Hope this helps!!


    BR, PM
    Monday, April 12, 2010 2:41 PM
  • Hi,

    If you have developed your Workflow using SharePoint Designer (SPD), you cannot implement any coding there. Means you cannot modify what you developed through SPD in Visual Studio.

    You have to re-write in Visual Studio and make your workflow. Hope this helps.


    BR, PM
    Tuesday, April 13, 2010 3:34 AM
  • You can check delete item permission in this way .....

    if (listitem.DoesUserHavePermissions(currentUser, SPBasePermissions.DeleteListItems))
    //delete item
    else
    //error message



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Tuesday, April 13, 2010 5:30 AM

All replies

  • Try this......................

    public override void ItemDeleting(SPItemEventProperties properties)
    {
         base.ItemAdded(properties);

    try
    {

        SPWeb web = _properties.OpenWeb();

          SPUser CurrentUser=web.SiteUsers[_properties.UserLoginName];

               if (!CurrentUser.IsSiteAdmin)  //Check whether user is administrator {               

                    _properties.Cancel = true;

                    _properties.ErrorMessage = "You are not authorized to delete the Item. Contact Administrator of site.";

                   
             }

    }
    catch (Exception ex)
    {

         // Log exceptions.........
    }

    }


    Regards, Aj (http://www.aj-sharepoint.blogspot.com/) MCTS
    Monday, April 12, 2010 2:37 PM
  • Hi,

    For creating the event handler and trapping the ItemDeleting event, try these links:

    http://www.wrox.com/WileyCDA/Section/Programming-Event-Handling-in-Windows-SharePoint-Services.id-306329.html

    http://msdn.microsoft.com/en-us/library/ms453149.aspx

    And, for checking permissions, you can write some code like given below:

    SPUser user = currentTeamSite.AllUsers.GetByID(properties.CurrentUserId);

    if (user.IsSiteAdmin)
    {
       //Do something 
    }
    else
    {
        properties.Cancel = true;
        properties.ErrorMessage = "Deleting items from " + properties.RelativeWebUrl + " is not supported.";

    }

    Hope this helps!!


    BR, PM
    Monday, April 12, 2010 2:41 PM
  • Hi Ajith

    i am having another question if you dont mind please

    i am having a workflow which actually moves item from one list to another list and regularly updates other lists when the item in another list is modified for all these actions i have to give users "Design" permission level. by giving design permission levels users can also do other stuff like design the site modifying webparts like that.

    i actually need all the users to be with only "Read" permission level but at the time when they approve or reject (custom column which is a dropdown box with approve, reject and pending ) the item i need to elevate their permissions from "Read" level to "Design" level and bring back to "Read" once the item deleted.

     

    i actually developed using Sharepoint Designer do you think  can we do anything in visual studio to modify them or we  have to develop everything again in visual studio 2008.

    thanks very much for your time.

     

    Monday, April 12, 2010 7:18 PM
  • Hi Prasanjit Mandal

    i am having another question if you dont mind please

    i am having a workflow which actually moves item from one list to another list and regularly updates other lists when the item in another list is modified for all these actions i have to give users "Design" permission level. by giving design permission levels users can also do other stuff like design the site modifying webparts like that.

    i actually need all the users to be with only "Read" permission level but at the time when they approve or reject (custom column which is a dropdown box with approve, reject and pending ) the item i need to elevate their permissions from "Read" level to "Design" level and bring back to "Read" once the item deleted.

    i actually developed using Sharepoint Designer do you think  can we do anything in visual studio to modify them or we  have to develop everything again in visual studio 2008.

     

    thanks very much for your time.

    Monday, April 12, 2010 7:18 PM
  • Hi,

    If you have developed your Workflow using SharePoint Designer (SPD), you cannot implement any coding there. Means you cannot modify what you developed through SPD in Visual Studio.

    You have to re-write in Visual Studio and make your workflow. Hope this helps.


    BR, PM
    Tuesday, April 13, 2010 3:34 AM
  • You can check delete item permission in this way .....

    if (listitem.DoesUserHavePermissions(currentUser, SPBasePermissions.DeleteListItems))
    //delete item
    else
    //error message



    Thanks
    Ganesh Jat [My Blog | LinkedIn | Twitter ]
    Tuesday, April 13, 2010 5:30 AM