Tag Info

Hot answers tagged

21

James and Toni are absolutely correct but have missed the obvious IMHO - your code has a deeper flaw and if you fixed this then disposing the objects is not required. You should (where possible) use the SPWeb and SPSite objects from SPContext - they are already created for you by the SharePoint web part infrastructure and its more efficient to use these ...


20

If you know the SPWeb but not the document library, you can use SPWeb.GetFile: SPFile file = web.GetFile(guid);


12

You have to instantiate new SPSite and SPWeb objects inside your RunWithElevatedPrivileges() delegate using the ID properties of the current context's SPSite and SPWeb objects: Guid siteId = SPContext.Current.Site.ID; Guid webId = SPContext.Current.Web.ID; SPSecurity.RunWithElevatedPrivileges(delegate() { using (SPSite site = new SPSite(siteId)) { ...


11

Yes, you will need to dispose SPWebs created through SPSite.OpenWeb(). To dispose of it, simple call web.Dispose() when you're done in the method. You could also use the Using statement to the same effect, without needing to directly call Dispose() Using site As New SPSite(SPContext.Current.Web.Url) Using web As SPWeb = site.OpenWeb() ...


10

Always pass it when possible but also make sure that any routines that you pass it to do not explicitly .Dispose() of it or use it in a using{} statement as that would Dispose() of the object earlier than expected. This would in turn cause all sorts of strange behavior for subsequent method calls.


9

Simpliest case Consider you know that document is stored in standard "Shared Documents" library from the Team Site Template, you should use this code: var library = web.GetListFromUrl("http://site/Shared%20Documents/Forms/AllItems.aspx"); var file = library.Items[new Guid("137DA01F-9AFD-5d9d-80C7-02AF85C822A8")].File; // file.Url - site-relative url of the ...


9

IF you get the SPWeb from the Context, it has been created elsewhere, and that elsewhere will destroy it for you. If you destroy (Dispose) it too early, when the page comes to Dispose of it, it'll most likely panic, and some data used to generate the page will probably be lost. Rule of thumb - if you use SPSite.OpenWeb() or otherwise use new SPWeb(), then ...


8

The key is if your code creates an instance of an SPSite or SPWeb you must dispose of it. However, if you use an instance from SPContext.Current you should not dispose of it. i.e. Dispose of what you create, whether that is directly or indirectly. Example of creating your own instance, so using is required: using(SPSite site = new SPSite("http://server")) ...


8

You do not need to call SPWeb.Update() because the AllowUnsafeUpdates property is not persisted. (Reference Best Pattern for AllowUnsafeUpdates)


8

Have a constructor on your job which takes in a SPWeb or string url, and then store the web url and list url and whatever other properties you want as a persisted property on the job. I recommend creating a web-scoped feature to install the timer job, and create it with a name that has the web ID tacked on (for uniqueness sake in case you want the job on ...


8

This is bad for 3 reasons: It will introduce a bug. static means it is shared across all threads for the life of the AppDomain. When user A goes to /web1 and user B goes to /web2 a second later, this code will try to execute as if it's in /web1. It could cause a memory leak. Holding a web any longer than absolutely necessary is almost always a really bad ...


7

+1 James You should also try to use SharePoint Dispose Checker tool in your projects to analyze your code for potential memory leaks and to apply best practices.


7

Yes, with SPWebApplication.Sites you get all site collections in a web application and with SPSite.AllWebs you get all sites in a site collection no matter the level. Best practice is to dispose explicitly of individual Web sites that are retrieved from the collection that is returned through the AllWebs property. foreach (SPWeb oweb in ...


6

I could be mistaken but I think he's referring to the creation of SPWeb objects when going through the allwebs enumeration. foreach(SPWeb web in SPContext.Current.Site.AllWebs) { try { //do something... } finally { web.dispose(); } } In this case I would dispose of the objects created but not this object: ...


6

Ideally, you should neither set or reset the value of AllowUnsafeUpdates. Instead use the SPUtility.ValidateFormDigest and that's it. This sets AllowUnsafeUpdates = true for the current HTTP request. The reason you should never set the AllowUnsafeUpdates = false is that other code might use SPUtility method an rely on that it was earlier asserted. Than your ...


6

That code will dispose the SPWeb object correctly with the using statement. If an exception is thrown in the method, it will still get disposed.


6

The various property bags are backed by Hashtable objects, except for SPWeb.Properties which is a StringDictionary (essentially a strongly-typed, non-generic Hashtable). SPWeb.AllProperties is a Hashtable and is prefered to AllProperties (it also does not force lowercase key values). The performance of all these containers is excellent (see for example ...


6

how about; warning: your feature needs to be scoped as web for it to work obviously ;) public override void FeatureActivated(SPFeatureReceiverProperties properties) { // No need to dispose the web istance, as indicated in the "Do not dispose" guidance SPWeb web = (SPWeb) properties.Feature.Parent ClassOfMine.doYourStuff(web); } not using ...


5

Nope, you're definitely right in what you say. Although Roger Lamb's post didn't seem to get updated with this, Stefan talked about it here over a year ago: Whenever an SPWeb object is created the SPSite object associated with the new SPWeb object automatically adds the new SPWeb object to an internal list. This works for all SPWeb object creations - be ...


5

A memory leak will manifest itself in whatever is the host process for the code which has the leak. In the case of SharePoint code, this won't be the SQL process - most likely what you're seeing here is simply down to the fact that SQL generally consumes as much memory as it can (i.e. whatever isn't being scavenged/used by other processes). You can peg it ...


5

There's the SPList.GetItemByUniqueId function, but if you don't know which list to search you're out of luck. If you're using SharePoint 2010, look at the Document ID functionality (which attaches a unique ID to a list item and then uses Search to find the item). Depending upon your requirements, you might consider implementing ...


5

I recommend installing and running this tool to check whether you are disposing your objects correctly: SPDisposeCheck


5

The Dispose() method if the IDisposal interface is a specifc interface that is used when object locks heavy and expensive resources. For instance when you're acquiring a connection to a database. The Dispose method is there to tell the object that I'm done with the connection (in the Sql case) and that you can now close it and let someone else use the ...


5

I am getting up to speed with SharePoint development as well and found Dispose Patterns By Example to be a great reference on this subject. Hope this helps you as much as it's helped me.


5

You can get WebTemplate and Configuration properties, then use string.format("{0}#{1}", web.WebTemplate, web.Configuration ).


5

Both SPSite and SPWeb objects implement the IDisposable interface. When the SPSite object finally gets disposed it will loop through the list and ensure that all SPWeb objects associated with this SPSite object also get disposed. This might lead to the assumption that just disposing all SPSite objects rather than disposing each individual SPWeb object would ...


5

Web Template: Web template refers to new feature element available in SharePoint 2010, which provides us flexible way to define definition (onet.xml file), which will be used only on provisioning time, when the site is created. There are no references to the definition on runtime, which provide easy maintainability for the definition. Source ...


4

The rule is complicated, but in a nutshell, you should dispose of any object that you create yourself that implements the IDisposable interface. In the context of SharePoint, this includes: Any SPSite object you create using siteColl.OpenWeb(), returned by SPSiteCollection.Add, by the SPSiteCollection[] index operator, or yielded in a SPSiteCollection ...


4

SPList objects cannot be disposed.



Only top voted, non community-wiki answers of a minimum length are eligible