Hot answers tagged

25

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


18

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


17

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; // added semicolon ClassOfMine....


12

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")) {...


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.


10

No need for looping , just get the library at a specific site based on the base type equal to DocumentLibrary then get count of collection as the following : using(SPSite site = new SPSite("https://yoursiteURL")) { using (SPWeb web = site.OpenWeb()) { SPListCollection libcol = Web.GetListsOfType(SPBaseType.DocumentLibrary); libcol.Count; } ...


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


9

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

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


8

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


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

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


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


6

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


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

You can try this- as bellow using(SPSite oSite = new SPSite("https://server/site")) { using (SPWeb oWeb = oSite.OpenWeb()) { SPListCollection docLibraryColl = oWeb.GetListsOfType(SPBaseType.DocumentLibrary); docLibraryColl.Count; } } Click Here & Here for reference. Hope this will help you!


6

SPWeb.Lists.Count.ToString(); is correct way to find number of Lists for any SPWeb. You might be seeing in difference in count you see here and in Site is because SharePoint has couple of hidden list like UserInformation list, managed metadata list which are hidden from UI. Also we can hide our custom list by using powershell/server side code. To give an ...


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

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


5

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


5

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


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

First of all, it doesn't have any to do with your code begin run in a web part (or any other specific position). What really matters here is that you are creating a new instance of a SPSite/SPWeb object. Since these classes both implement the IDisposable interface, and since you are creating the instances yourself (in contrast to getting them from another ...


5

All permissions required to correct this error: Site Collection administrator on the SPSite (whether given through Central Admin or Site Settings) PowerShell scripting admin: add-spshelladmin domain\username PowerShell scripting admin on the content databases: get-spcontentdatabase | add-spshelladmin domain\username note that the powershell commands must ...


4

SPList objects cannot be disposed.


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

You can use the full URL to the web. When getting an SPWeb you have two options: 1) us the SPSite and parse through the Webs -or- 2) look it up by the full URL to the web.


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 }


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