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 ...


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

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

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

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 ...


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

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 ...


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 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

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

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

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

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); } if not then ...


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

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 ...


4

If you are in doubt as to whether or not you are handling the dispose correctly, Microsoft has the SPDisposeCheck tool available to check it for you. You can run this manually, directly from Visual Studio or even incorporate it into the validation on your build server.


4

Thanks, it helped us a lot..saved a lot of time. Great answer :) From SPSecurity.RunWithElevatedPrivileges: "An SPSite object created outside the delegate can be referenced inside the delegate, however, the methods and property assessors of the object run with the privileges of the user context in which the objects were created, not with the elevated ...


4

The SPList.Folders returns the collection of SPListItem object. so we have to iterate the SPList.Items and then we have get the folder object from SPListItem. We have to replace the following line, foreach (SPFolder oFolder in list.Folders) with this snippet. foreach (SPListItem item in list.items) { SPFolder oFolder = item.Folder; // Your code } ...


4

On SharePoint2007, you cannot access SharePoint object like SPWeb from JavaScript. Only on SharePoint 2010, the client object model was added that enables interaction with the SPWeb object (see this link) Specifically, the easiest solution for the problem of sending an email from client side code is wtriting an Ajax callback, the client code uses the ...


4

Very similar to above, the SPSite object does actually remember the URL it was opened with - so you don't have to give a Web name. You should be able to use something like: string url= "Full URL to File"; using (SPSite site = new SPSite(url)){ using (SPWeb web = site.OpenWeb()) { SPFile file = web.GetFile(url); SPListItem item = ...


4

SPContext.Current.Site.OpenWeb(relative_web_url) won't ever be the same as the current context. It might be a second SPWeb object that corresponds to the same site as the current context, but it's not the same actual object instance, so it's always safe to dispose of (and important to always dispose of) the SPWeb object returned from every call to OpenWeb. ...


3

Theres nothing like this OOB, but you have pretty much the same options in PowerShell as in say a console application in C#. So theres the option of coding your own cmdlet that does this for you. On NBSP i found this script that should get you going.


3

There is a best practices guide from microsoft about when to dispose which objects (SPSite, SPWeb): http://msdn.microsoft.com/en-us/library/ee557362(v=office.14).aspx Also there is a dispose checker tool provided by microsoft: http://archive.msdn.microsoft.com/SPDisposeCheck


3

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 ...



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