c# - Using Disposable With SharePoint Objects (Web Parts) -


While writing web parts in SharePoint, do you implement IDisposable via the using the clause are doing? Or, can you remove your items in try / catch / end ? Both depends on? Why do you choose one on another?

Background:

I am working on a legacy code that does not apply IDisposable, so I am reading MSDN's "best practice" to fix the problem. There is a good article covering:

It recommends using , but then it goes on to say that in many cases it Using to use is not appropriate.

I can understand futility in this article - the real answer is clear as sludge :) < / P>

When it comes to web part development (or developmentally common with SharePoint API), settlement of certain objects (in particular, SPOM objects) will give unexpected results, you do not install your object correctly Are there.

Specifically, if you obtain an object reference from SPS ite or SPWB from the current context and try to settle it (either with an experiment segment or at the end), you will get SharePoint Receive an object reference error from the stack.

The correct way to settle your SP objects (SPS ite, SPWeb, etc.) is to present a new SP object to get the reference) from the site using a URL. For example:

  (SPSite siteCollection = New SPS using ite ("http: // your_site_url")) (SPWeb site = siteCollection.OpenWeb ("News")) // {do Stuff with your news}}  

Here is an example of a code that will throw an object reference error if you try to use it:

  Using (context site) = SPControl.GetContextSite (context)) {SPWeb site = siteCollection.OpenWeb ("News")) {// do stuff with your news}}  

Error will not use block round It happens, but will be downstream later when SharePoint tries to do something internally with the site.

In terms of general use, I am trying to use () after trying / trying a great fan. You have got an IDisposable object after all I chose the option to try / finally If I have to make extra arguments to de-object the non-id-compatible object.

Hope this will help!


Comments

Popular posts from this blog

python - Overriding the save method in Django ModelForm -

html - CSS autoheight, but fit content to height of div -

qt - How to prevent QAudioInput from automatically boosting the master volume to 100%? -