{"id":9,"date":"2007-09-27T01:00:00","date_gmt":"2007-09-27T06:00:00","guid":{"rendered":"http:\/\/blogs.devhorizon.com\/reza\/?p=9"},"modified":"2008-03-08T15:46:22","modified_gmt":"2008-03-08T20:46:22","slug":"item-level-permission-in-workflow-createtaskxxx-activities","status":"publish","type":"post","link":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/","title":{"rendered":"Item Level Permission in workflow CreateTaskXXX activities"},"content":{"rendered":"<p>Both CreateTask and CreateTaskWithContentType\u00a0 activities\u00a0 have a property called &#8221; SpecialPermissions &#8221; that takes a hashtable of key-value pairs of type string and SPRoleType. Setting the SpecialPermissions property\u00a0 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 .<\/p>\n<blockquote><p>\u00a0\u00a0 private void createTask(object sender, EventArgs e)<br \/>\n {<br \/>\n &#8230;&#8230;.<br \/>\n CreateTask task1 = sender as CreateTask;<br \/>\n HybridDictionary permsCollection = new HybridDictionary();<br \/>\n permsCollection.Add(taskProps.AssignedTo, SPRoleType.Administrator);<br \/>\n task1.SpecialPermissions = permsCollection;<br \/>\n }<\/p><\/blockquote>\n<p> Alternatively, If you are using &#8220;CreateTaskWithContentType&#8221; activity, you have one other option : The event handler bound to your content type. Here is how I&#8217;d do it:<\/p>\n<p> 1) I create the Task using &#8220;CreateTaskWithContentType&#8221; activity from my workflow. Obviously, I already have a content type in place.<\/p>\n<p> 2) I have also attached an event handler to the content type I use above. Oops! I forgot to say that there is no way you can visually do that (except <a href=\"http:\/\/www.codeplex.com\/SPSCustomAdmin\/\">this one<\/a>), so I use a programmatic approach to assign an event handler to my content type when the feature that contains my content type get activated.<\/p>\n<p> 3) So I have to create a Feature that contains the content type (MyTaskContentType.xml is content type definition which I will skip for the sake of code brevity)<\/p>\n<blockquote><p>&lt;?xml version=&#8221;1.0&#8243; encoding=&#8221;utf-8&#8243; ?&gt;<br \/>\n &lt;Feature Id=&#8221;5FED1202-C6B8-453e-9EC0-F328655ED4DC&#8221;<br \/>\n Title=&#8221;My Workflow Task Content Types&#8221;<br \/>\n Description=&#8221;&#8230;.&#8221;<br \/>\n Version=&#8221;12.0.0.0&#8243;<br \/>\n Scope=&#8221;Site&#8221;<br \/>\n xmlns=&#8221;<a href=\"http:\/\/schemas.microsoft.com\/sharepoint\/\">http:\/\/schemas.microsoft.com\/sharepoint\/<\/a>&#8221;<br \/>\n ReceiverAssembly=&#8221;DevHorizon.SharePoint.Workflows, Version=1.0.0.0, Culture=neutral, PublicKeyToken=207a12b7817b805c&#8221;<br \/>\n ReceiverClass=&#8221; &#8220;DevHorizon.SharePoint.Workflows.AddConentTypesEventHanlders&#8221;&gt;<br \/>\n &lt;ElementManifests&gt;<br \/>\n &lt;ElementManifest Location=&#8221;DivRepTask.xml&#8221; \/&gt;<br \/>\n &lt;ElementManifest Location=&#8221;HRAdminTask.xml&#8221; \/&gt;<br \/>\n &lt;\/ElementManifests&gt;<br \/>\n &lt;\/Feature&gt;<\/p><\/blockquote>\n<p> 4) I create a feature receiver class for this feature, I get a reference to the content type and attach the event handler to it programmatically:<\/p>\n<blockquote><p>public override void FeatureActivated(SPFeatureReceiverProperties properties)<br \/>\n {<br \/>\n using (SPSite siteCollection = (SPSite) properties.Feature.Parent)<br \/>\n {<br \/>\n using (SPWeb web = siteCollection.OpenWeb())<br \/>\n {<br \/>\n System.Reflection.Assembly a = System.Reflection.Assembly.GetExecutingAssembly(); SPContentType ctMyTask = web.ContentTypes[&#8220;My Conent Type&#8221;];<br \/>\n ctMyTask.EventReceivers.Add(SPEventReceiverType.ItemAdded, a.FullName,&#8221;MyTaskItemEventReceiver&#8221;);<br \/>\n ctMyTask.Update();<br \/>\n web.Update();<\/p>\n<p> }<br \/>\n }<br \/>\n }<\/p><\/blockquote>\n<p> 5) Now,I need to create an event handler class named &#8220;MyTaskItemEventReceiver&#8221; that inherits from SPItemEventReceiver.Make sure you decorate this class with the right attributes to target to the featureId that contains your content type and the content type Id that you are writing this event handler for. You also need a unique GUID for your event handler<\/p>\n<blockquote><p>[TargetContentType(&#8220;feature id goes here&#8221;, &#8220;content type id goes here&#8221;)]<br \/>\n [Guid(&#8220;and here is the unique id for your enevnt handler&#8221;)]<\/p><\/blockquote>\n<p> 6) In ItemAdded method of your event handler add the following code:<\/p>\n<blockquote><p>DisableEventFiring();<br \/>\n SPListItem listItem = properties.ListItem;<br \/>\n try<br \/>\n {<br \/>\n listItem.BreakRoleInheritance(false);<br \/>\n listItem.Update();<br \/>\n listItem =SetItemLevelPermissions(listItem.Web, listItem, SPRoleType.Contributor, listItem[&#8220;Assigned To&#8221;].ToString());<br \/>\n listItem.Update();<br \/>\n }<br \/>\n catch (Exception ex)<br \/>\n {<br \/>\n \/\/Add error handling code here<br \/>\n }<br \/>\n EnableEventFiring();<\/p><\/blockquote>\n<p> 7) And finally add this helper method somewhere accessible from within your event handler<\/p>\n<blockquote><p>public static SPListItem SetItemLevelPermissions(SPWeb setSPWeb, SPListItem setListItem, SPRoleType setRoleType, string assignedTo)<br \/>\n {<br \/>\n int index = assignedTo.IndexOf(&#8216;;&#8217;);<br \/>\n int id = Int32.Parse(assignedTo.Substring(0, index));<br \/>\n SPUser user = setSPWeb.SiteUsers.GetByID(id);<br \/>\n SPRoleDefinition roleDefinition = setSPWeb.RoleDefinitions.GetByType(setRoleType);<br \/>\n SPRoleAssignment roleAssignment = new SPRoleAssignment(user.LoginName, string.Empty, string.Empty, string.Empty);<br \/>\n roleAssignment.RoleDefinitionBindings.Add(roleDefinition);<br \/>\n setListItem.RoleAssignments.Add(roleAssignment);<br \/>\n return setListItem;<br \/>\n }<\/p><\/blockquote>\n<p> Now when your workflow creates a task, its item level permission will be broken and set to &#8220;assigned to&#8221; field.Happy ending!<\/p>\n<p> <img loading=\"lazy\" decoding=\"async\" src=\"\/\/blogs.devhorizon.com\/aggbug.aspx?PostID=508\" height=\"1\" width=\"1\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Both CreateTask and CreateTaskWithContentType\u00a0 activities\u00a0 have a property called &#8221; SpecialPermissions &#8221; that takes a hashtable of key-value pairs of type string and SPRoleType. Setting the SpecialPermissions property\u00a0 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[43],"tags":[3],"class_list":["post-9","post","type-post","status-publish","format-standard","hentry","category-moss-2007","tag-moss-2007"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Item Level Permission in workflow CreateTaskXXX activities - Reza Alirezaei&#039;s Blog %<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Reza Alirezaei\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/\"},\"author\":{\"name\":\"Reza Alirezaei\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"headline\":\"Item Level Permission in workflow CreateTaskXXX activities\",\"datePublished\":\"2007-09-27T06:00:00+00:00\",\"dateModified\":\"2008-03-08T20:46:22+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/\"},\"wordCount\":576,\"commentCount\":11,\"keywords\":[\"MOSS 2007\"],\"articleSection\":[\"MOSS 2007\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/\",\"name\":\"Item Level Permission in workflow CreateTaskXXX activities - Reza Alirezaei's Blog %\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\"},\"datePublished\":\"2007-09-27T06:00:00+00:00\",\"dateModified\":\"2008-03-08T20:46:22+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/2007\\\/09\\\/27\\\/item-level-permission-in-workflow-createtaskxxx-activities\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#website\",\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/\",\"name\":\"Reza Alirezaei's Blog\",\"description\":\"Blogging from the field!\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/#\\\/schema\\\/person\\\/cdbb24d283697a65951cb4a14e474938\",\"name\":\"Reza Alirezaei\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g\",\"caption\":\"Reza Alirezaei\"},\"url\":\"https:\\\/\\\/blogs.devhorizon.com\\\/reza\\\/author\\\/rezaa\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Item Level Permission in workflow CreateTaskXXX activities - Reza Alirezaei's Blog %","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/","twitter_misc":{"Written by":"Reza Alirezaei","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/#article","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/"},"author":{"name":"Reza Alirezaei","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"headline":"Item Level Permission in workflow CreateTaskXXX activities","datePublished":"2007-09-27T06:00:00+00:00","dateModified":"2008-03-08T20:46:22+00:00","mainEntityOfPage":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/"},"wordCount":576,"commentCount":11,"keywords":["MOSS 2007"],"articleSection":["MOSS 2007"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/","url":"https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/","name":"Item Level Permission in workflow CreateTaskXXX activities - Reza Alirezaei's Blog %","isPartOf":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#website"},"datePublished":"2007-09-27T06:00:00+00:00","dateModified":"2008-03-08T20:46:22+00:00","author":{"@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blogs.devhorizon.com\/reza\/2007\/09\/27\/item-level-permission-in-workflow-createtaskxxx-activities\/"]}]},{"@type":"WebSite","@id":"https:\/\/blogs.devhorizon.com\/reza\/#website","url":"https:\/\/blogs.devhorizon.com\/reza\/","name":"Reza Alirezaei's Blog","description":"Blogging from the field!","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blogs.devhorizon.com\/reza\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/blogs.devhorizon.com\/reza\/#\/schema\/person\/cdbb24d283697a65951cb4a14e474938","name":"Reza Alirezaei","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/3ba940d84e0ecb909e62e93df4c56daf0395c7e53c914467ab2ee73124a7d7b6?s=96&d=mm&r=g","caption":"Reza Alirezaei"},"url":"https:\/\/blogs.devhorizon.com\/reza\/author\/rezaa\/"}]}},"_links":{"self":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/9","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/comments?post=9"}],"version-history":[{"count":0,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/posts\/9\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/media?parent=9"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/categories?post=9"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.devhorizon.com\/reza\/wp-json\/wp\/v2\/tags?post=9"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}