Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property


I was writing a workflow using SharePoint designer wherein at certain steps tasks were getting created and assigned to different user. But the problem with that was that any user having appropriate rights on the tasks list was able to edit the task.

Below are the two methods using which we can have only the assigned to user having the rights on that task.

It can be done using Event Receiver or within the SharePoint workflow using special permissions property.

public override void ItemAdded(SPItemEventProperties properties)

        {

                // Name of the List

            if (properties.ListTitle == “Tasks”)

            {     

                // Get the SPSite Object

                SPSite objSite = new SPSite(http://servername:portname”);       

                // Point to the top level web site within it

                SPWeb objWeb = objSite.OpenWeb();

                // get the task list item getting created

                SPListItem myListItem = properties.ListItem;       

 

                // get the id of the assigned to user

                // we want that only assigned to user should have full rights on that task

                string userAssignedTo=myListItem[“Assigned To”].ToString();

                int index = userAssignedTo.IndexOf(‘;’);

                int id = Int32.Parse(userAssignedTo.Substring(0, index));

                // get the SPUser from the id

                SPUser user = objWeb.SiteUsers.GetByID(id);                       

 

                // break the role inheritance

                myListItem.BreakRoleInheritance(false);

                // webroledefinitions – Full Right, Design, Contribute and Read

                SPRoleDefinitionCollection webroledefinitions = objWeb.RoleDefinitions;

                SPRoleAssignment roleassignment = new SPRoleAssignment(user);              

                roleassignment.RoleDefinitionBindings.Add(webroledefinitions[“Full Control”]);

                myListItem.RoleAssignments.Add(roleassignment);

                // give full control right to the assigned to user

                roleassignment.Update();                           

             

 

               }

            }

Or within workflow as

 

    // handler for create task activity

        private void createTask1_MethodInvoking(object sender, EventArgs e)

        {

            //Specify properties for the task

            createTask1.TaskProperties.AssignedTo = @”domainusername”;

            createTask1.TaskProperties.Title = @”Please complete the task”;

            createTask1.TaskProperties.Description = “This is sample SharePoint Task”;

            createTask1.TaskProperties.DueDate = DateTime.Now.AddDays(7);

            createTask1.TaskProperties.EmailBody = “This is the sample<b><i> email body </b></i>”;

            createTask1.TaskProperties.SendEmailNotification = true;

 

            // Define a HybridDictionary object

            HybridDictionary permsCollection = new HybridDictionary();

            // Give Administrator rights to the user to whom the task has been assigned

            permsCollection.Add(createTask1.TaskProperties.AssignedTo, SPRoleType.Administrator);

            // SpecialPermissions -the SpecialPermissions property  in your code will strip out all existing permissions inherited from

            // the parent list(Workflow Task List) and only adds permissions for each pair you added to the hashtable

            createTask1.SpecialPermissions = permsCollection;   

 

        }

 

 

That’s it ….


Discover more from Nishant Rana's Weblog

Subscribe to get the latest posts sent to your email.

Unknown's avatar

Author: Nishant Rana

I love working in and sharing everything about Microsoft.NET technology !

13 thoughts on “Managing Tasks Permissions Programmatically within SharePoint using event reciever or using special permissions property”

  1. Iam facing the same problem any user can access others task, where to put the code provided ,do it like web part??

    can u give me more details

    Like

  2. Nishant,

    Please let me know how to show only tasks assigned for current user only, I want to hide all others tasks.

    Also where to add this code? Please provide step by step details. My workflow is designed in SPD.

    Like

  3. Thank you so much for this post! One question..the event receivers, do you have to implement this as a feature?

    Like

  4. thank you for your post
    but after i add the event receiver users can not add task item it give me error

    The form cannot be submitted.
    The form cannot be submitted to the specified SharePoint list or document library. The SharePoint location may be read-only or you may not have permissions to access it.

    Like

  5. to set edit permission only to task “AssignedTo” user can be achived by associating of a simple workflow to Task /Workflow task list.

    SPD has an activity called “Grant” and “Remove” Permission. This can be used to set permission only to “Assigned To” user or Group

    Like

  6. The code below is not working. Permission not set to the specified user. Any idea ?

    // handler for create task activity
    private void createTask1_MethodInvoking(object sender, EventArgs e)
    {
    //Specify properties for the task
    createTask1.TaskProperties.AssignedTo = @”domainusername”;
    createTask1.TaskProperties.Title = @”Please complete the task”;
    createTask1.TaskProperties.Description = “This is sample SharePoint Task”;
    createTask1.TaskProperties.DueDate = DateTime.Now.AddDays(7);
    createTask1.TaskProperties.EmailBody = “This is the sample email body ”;
    createTask1.TaskProperties.SendEmailNotification = true;

    // Define a HybridDictionary object
    HybridDictionary permsCollection = new HybridDictionary();
    // Give Administrator rights to the user to whom the task has been assigned
    permsCollection.Add(createTask1.TaskProperties.AssignedTo, SPRoleType.Administrator);
    // SpecialPermissions -the SpecialPermissions property in your code will strip out all existing permissions inherited from
    // the parent list(Workflow Task List) and only adds permissions for each pair you added to the hashtable
    createTask1.SpecialPermissions = permsCollection;

    }

    Like

Leave a reply to kiran Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Nishant Rana's Weblog

Subscribe now to keep reading and get access to the full archive.

Continue reading

Power Platform Puzzles

D365 CRM, Power Platform Tips &Tricks

Power Spark

Power Spark By Shrangarika

Van Carl Nguyen

Exploration of Power Platform

My Trial

It is my experience timeline.

Power⚡Thomas

Sharing my knowledge and experience about the Microsoft Power Platform.

Arpit Power Guide

a guide to powering up community

Welcome to the Blog of Paul Andrew

Sponsored by Cloud Formations Ltd

Deriving Dynamics 365

Deriving Solutions and features on Power Platform/Dynamics 365

The CRM Ninja

Thoughts & musings from a Microsoft Business Applications Ninja!

D CRM Explorer

Learn about Microsoft Dynamics CRM Power Platform customization and implementation and other cool stuffs

Stroke // Jonas Rapp

I know pre-stroke. I will improve who I was.

Power Melange

Power Melange By Shalinee

Clavin's Blog - PPUG.ORG

AI - Power Automate - Power Apps - SharePoint Online - Azure - Nintex - K2 - Artificial Intelligence

Sat Sangha Salon

An Inquiry in Being

The Indoencers

The Influencers & Influences of Indian Music

Monika Halan's blog

Hand's-free money management

D365 Demystified

A closer look at Microsoft Dynamics 365.

Microsoft Mate (msftmate) - Andrew Rogers

Experienced consultant primarily focused on Microsoft Dynamics 365 and the Power Platform

Manmit Rahevar's Blog

One Stop Destination for Microsoft Technology Solutions

MG

Naturally Curious

Brian Illand

Power Platform and Dynamics 365

Steve Mordue

The Professional Paraphraser

Subwoofer 101

Bass defines your home theater

SQLTwins by Nakul Vachhrajani

SQL Server tips and experiences dedicated to my twin daughters.

Everything D365

Discovering Azure DevOps and D365 Business Applications

Tech Wizard

Lets do IT Spells

XRM Tricks (Power Platform & Dynamics CRM )

Power Platform & Dynamics CRM

CRM TIPS BY PRM

Mail to crmtipsbyprm@gmail.com for queries and suggestions

nijos.dev

Giving back to the community what I have learned

Power Platform Learning

Your Go-To Resource for Power Apps, Power Automate & More

xrm CRM Dynamics

Dynamics CRM Technical & Functional Info

Dynamics 365 Blogs - Explained in unique way

Sometimes you need to look at things from different perspective.