zoukankan      html  css  js  c++  java
  • 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;   

            }

  • 相关阅读:
    SQL语句 分页实现
    PHPexcel入门教程
    json_decode返回null,使用echo json_last_error(); 返回4
    配置mysql可局域网内访问
    thinkphp5.0安装composer安装topthink/think-captcha
    linux下mysql忘记密码怎么办
    centos7 设置mysql账号密码开放3306端口实现远程登陆
    高级数据结构第七章A . 数列(树状数组逆序对)
    高级数据结构第六章E . 苹果树 (dfs+树状数组)
    高级数据结构第六章C . 奶牛狂欢节(两个树状数组)
  • 原文地址:https://www.cnblogs.com/icedog/p/1776894.html
Copyright © 2011-2022 走看看