BreakRoleInheritance and Dispose

by | Aug 26, 2010 | Development, SharePoint | 0 comments

I’ve wrote some piece of code which was used to clear security setting on item level in the library:

private void UsunDziedziczenieUprawnien_ExecuteCode(object sender, EventArgs e)
{
using (SPSite site = new SPSite (workflowProperties.SiteId))
{
using (SPWeb web = site.OpenWeb(workflowProperties.WebId))
{
workflowProperties.Item.BreakRoleInheritance(true );
SPGroup MyGroup = web.SiteGroups[“MyGroup” ];
workflowProperties.Item.RoleAssignments.Remove(MyGroup);
workflowProperties.Item.Update();
} } }

It looks simple, and nothing special. But each time this method has been lunched I get errors in SharePoint log files: “ERROR: request not found in the TrackedRequests. We might be creating and closing webs on different threads … “. And after that was entry:

An SPRequest object was not disposed before the end of this thread.  To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.  This object will now be disposed.  Allocation Id: {35AEF0A1-C868-4205-9CCE-D557E252AFA8}  To determine where this object was allocated, create a registry key at HKEY_LOCAL_MACHINESOFTWAREMicrosoftShared ToolsWeb Server ExtensionsHeapSettings.  Then create a new DWORD named SPRequestStackTrace with the value 1 under this key.

What’s going on? Thanks to response of Wictor Wilén on at MDSN forums I found out that if your code contains BreakRoleInheritance you should use Dispose on ParentWeb . Well, I didn’t know that. And as you noticed this code use SPWeb and SPSite in way of best practices presented on MSDN – they were disposed. But not BreakRoleInheritance … Below code which is work fine for me

private void UsunDziedziczenieUprawnien_ExecuteCode(object sender, EventArgs e)
{

using (SPSite site = new SPSite(workflowProperties.SiteId))
{
using (SPWeb web = site.OpenWeb(workflowProperties.WebId))
{
SPList lista = workflowProperties.List;
SPListItem element = workflowProperties.Item;
SPGroup myGroup = web.SiteGroups[“MyGroup”];
try
{
element.BreakRoleInheritance(true);
element.RoleAssignments.Remove(myGroup);
element.Update();
}
finally
{
lista.ParentWeb.Dispose();

} }

Written by Tomasz Szulczewski

Hi, my name is Tomasz Szulczewski, and I have been in love with information technology for over 25 years, but I still have an IT passion and feel like a geek. I am a person who is problem solver who thinks that not all people must be experts in IT.

Related Posts

What is SharePoint in Office 365?

What is SharePoint in Office 365?

What is SharePoint in Office 365, and why is SharePoint for small business the critical product? So you bought a Microsoft 365 license, and your tenant is online. You open a browser, and you have no idea what to do next? Don't worry; I will try to give you a few...

read more
Hubfly

Hubfly

A few days ago I have a chance to play with SharePoint online solution called Hubfly and I was really impressed with his capabilities. And here is my review. So let’s start from the begging. I wrote a few times on this blog that the main problem with SharePoint is...

read more

0 Comments

Leave a Reply

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