• Skip to main content
  • Skip to primary sidebar
  • Skip to footer
  • Technology
    • Development
    • Hardware
    • Intune
    • Microsoft
    • Office
    • Office 365
    • Security
    • SharePoint
    • Software
    • SQL server
  • Personal development
    • Blog
    • Career
    • Freelancer
    • Knowledge
    • NEWS
    • Private
    • Thoughts
  • Consulting services
  • Contact me

Not only IT

About life, IT and other things...

Home » Technology » Development » BreakRoleInheritance and Dispose

BreakRoleInheritance and Dispose

August 26, 2010 By Tomasz Szulczewski 1 Comment

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();

} }

Related

Filed Under: Development, SharePoint Tagged With: SharePoint

About Tomasz Szulczewski

I've got more than 20 years of IT experience. IT is my passion and I am still increasing my skills. I work as a SharePoint, Office 365 and Azure architect.

Reader Interactions

Trackbacks

  1. BreakRoleInheritance in SharePoint 2010 says:
    June 2, 2014 at 2:55 pm

    […] you remember my post about problems with BreakRoleInheritance  in SharePoint 2007 (here)? There were errors messages that I didn’t dispose SPSite and SPWeb objects after usage. […]

    Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Join the Newsletter

Social media

  • Facebook
  • Instagram
  • LinkedIn
  • Twitter
  • YouTube

Search this site

Sign up for Wise accounts

Signup and earn $25

payoneer
payoneer

My latest achievement

Microsoft 365 Certified: Enterprise Administrator Expert
Microsoft 365 Certified: Enterprise Administrator Expert

Tags

Azure Azure Active Directory BizSpark Blog career Certification cloud conference edge freelance Freelancer Hardware home office InfoPath Intune Knowledge licensing Microsoft Microsoft 365 News Office Office 365 Personal development Power virtual agent Private security SharePoint SharePoint designer SharePoint online Software SQL server upwork Windows Windows 365 yammer

Footer

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy

Copyright © 2022 Tomasz Szulczewski