Tag Info

Hot answers tagged

25

Absolutely, if you have SPContext.Current, you don't need to dispose any objects inside. The point is, that SPContext.Current is binded to a SPRequest object, which represents current web page request. So basically, if you will dispose SPContext.Current.Web in your code, it will no longer be available for any other pending code. But, if you will create your ...


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


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


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

http://msdn.microsoft.com/en-us/library/ff458385.aspx: {SiteUrl} – The fully qualified URL to the site (Url). The SharePoint code proves it - here's the SPCustomActionElement.ReplaceUrlTokens method: internal static string ReplaceUrlTokens(string urlAction, SPWeb web, SPList list, SPListItem item) { if (string.IsNullOrEmpty(urlAction)) ...


8

SPContext and HttpContext are very distinct objects. SPContext Represents the context of an HTTP request in Microsoft SharePoint Foundation. HttpContext Encapsulates all HTTP-specific information about an individual HTTP request. If you look at their members and properties, you will see that SPContext will allow you to access the SharePoint Object Model ...


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.


6

In IE (version > 6) SignOut.aspx uses ClearAuthenticationCache as a client side command. This does not work for Firefox. I found this example on StackOverflow that might work for you http://stackoverflow.com/questions/31326/is-there-a-browser-equivalent-to-ies-clearauthenticationcache


5

Try SPContext.Current.Site.RootWeb.RootFolder.WelcomePage


5

Maybe SPServer.Local is what you are looking for...


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


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

It would be cool if you posted the offending code and its surroundings, but some things to consider out of the top of my head: You might be getting the object too early in the lifecycle (say PreInit methods and such, or an HttpModule etc) You might be breaking the pipeline doing things like setting the HttpContext to null (I've seen that in SharePoint ...


4

I am answering my questions. After reviewing @SimonDoy comment that "That's strange" and realized I am really making some mistake and then realize mistake. I made mistake, I was running with elevated permission and within that I was referencing SPContext.Current. The error code public void ProcessRequest(HttpContext context) { ...


3

Workflows do not have an SPContext so SPContext.Current will always be null. In order to do what you appear to be trying to do, you may have to open the site once as the AppPool account to get the user token then release the site. Then open it again using the token like you are doing now. Something like this : SPUserToken token = null; using (SPSite ...


3

Using {SiteUrl} token is nice, thanks to Alex Boev. Just to mention an alternative, you can use SharePoint javascript API to get the web url. In your example you want to open an application page (from layouts folder). There is a helper function for doing that getLayoutsPageUrl: SP.Utilities.Utility.getLayoutsPageUrl('myPath/EditWebInfo.aspx') So your ...


3

Arrrrrgh! Finally figured it out after messing with this for a good day. I want to leave this here just in case anyone else runs into the same problem. When I register the Javascript endpoint, I use the full URL of the service. So if the root of my site is at: http://mydomain.com/sites/site1 I register it at: ...


3

You actually need to generate a SP.ClientContext for the app web and a SP.AppContextSite for the host web. To get a reference to a list in the host web, try the following: // Get the ClientContext for the app web clientContext = new SP.ClientContext.get_current(); // Use the host web URL to get a parent context - this allows us to get data from the parent ...


3

What about that? $('.s4-ctx').css('display','none'); It will hide buttons that show context menu. If you need to hide context menu only from web part with specified id you need to use: $("div[webpartid='85c13a33-57a3-456b-a19b-c68c97756184'] .s4-ctx").css('display','none'); If you want to remove menu item, for ex. "Alert Me" add this script that ...


3

You need to have app web for that. In provider hosted app, app web is not present by default. Create an empty module or list in your app web; basically in SP project which gets created when you create a provider hosted app and then you will have context. Besides that, why do you need context of app web of you don't have anything in there?


2

Another reason for using the SPContext.GetContext is to set the context of a control to one that is different than the context of the current page. For example, you can set the ItemFieldIterator.ItemContext proprty to get information from a list on different SPWeb object. myItemFieldIterator.ItemContext = SPContext.GetContext(Context, 0, ListId, oSPWeb);


2

To get full url (absolute), you have to : SPWeb parentWeb=SPContext.Current.Site.RootWeb; string strFullHomePageUrl= parentWeb.Url+ "/"+ parentWeb.RootFolder.WelcomePage;


2

The L_Menu_BaseUrl variable is what you are looking for. SharePoint stores the current web url in it. Have a look here: http://vrdmn.blogspot.in/2011/08/javascript-lmenubaseurl-varaible-for.html Also some additional info on other OOB JavaScript Objects: _spPageContextInfo.webServerRelativeUrl will also give you the current web url. ...


2

As far as I know, changing field type from text to lookup is not supported in Sharepoint. You should create new lookup field in the Attendees list and point it to Events list, either from Sharepoint list settings, or from code. Basically, lookup field has the same structure as user field, which you populated in the final bit of your code - ID#;Title. Also, ...


2

No, there will not be an error. I commonly use this design pattern to keep my using blocks small. SPList list; using (SPWeb web = SPContext.Current.Site.OpenWeb("myweb")) { list = web.GetList("/myweb/Lists/Awesome Name"); } SPListItemCollection items = list.Items; If you are updating an SPListItem in a manner that requires SPWeb.AllowUnsafeUpdates = ...


2

In addition to the method with determining ContentType in custom ListFieldIterator from query string, ItemContext contains property ContentType. But this property is internal and therefore could not be used directly in custom ListFieldIterator implementation. Below is presented wrapper for retriving ContentType based on corresponding property for SPContext ...


2

I have only experienced this when I forget that I'm calling it out of context. are you sure you are not trying to call this within SPSecurity.RunWithElevatedPrivileges() ?


2

I managed to get this working using promises, code below: var queryItems = function(myContext, myDocName, mySelector) { var dfd = $.Deferred( function() { var inContxt = myContext, inDoc = myDocName, myCtx = new SP.ClientContext(inContxt), myList = myCtx.get_web().get_lists().getByTitle(inDoc), ...


2

Any reason why you are not using a HideCustomAction? Here is the MSDN for that. And here is a resource for finding the correct groups



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