A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

Hot answers tagged

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


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

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


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

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

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

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

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


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

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

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

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

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


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


4

If you want to get hold of a subsite using GUID or URL (server-relative or site-relative), OpenWeb() should be used.The SPWeb returned from OpenWeb() should be manually disposed. Also,look this article for more information on OpenWeb() http://blog.falchionconsulting.com/index.php/2009/03/why-i-dont-use-openweb/. RootWeb gives you the top most SPWeb ...


4

This is because of the SPWeb.Lists.Count.ToString(); return the number of all lists and libraries not only lists To can get the accurate number of lists only you should exclude the library and other list types from your code as the following int listcount=0; foreach (SPList list in Web.Lists) { if (!list.Hidden & list.BaseType != ...


4

No, you should not use a using as you're not instantiating the object - you're getting a reference to an existing object. Let the event receiver handle the life cycle of its own objects.


3

Someone has already done what you want, in a much better way. In fact, you're using it in your example code: public static SPWeb WebObject() { if (webObject != null) return webObject; else if (SPContext.Current != null) { webObject = SPContext.Current.Web; } return webObject; } You are using SPContext.Current.Web as a ...


3

I just ran into the same problem and I figured out that you can pass the entire URL into the constructor of SPSite and it will determine which portion of the url is the site collection. Here is my code snippet I used in a method where I am trying to seperate the string into the site collection url and the relative site path. try { //these local ...


3

I think you have to dispose the SPWeb objects when retrieved from the SPSite.AllWebs collection. If you have look at the implementation of the SPWebCollection class by decompiling it, you can see that everytime a SPWeb is returned, it is instantiated by calling OpenWeb on the SPWebCollection.ISPWebCollectionProvider (which is in this case the SPSite). Thus, ...


3

I would strongly recommend you go through this reference article if you haven't done so already.


3

As Hugh said, in the getSelectedList function you have the first error. using (myWeb) { SPListCollection myLists = myWeb.Lists; foreach (SPList myList in myLists) { if (myList.Title.Equals(ListTitle)) rList = myList; } } myWeb.Dispose(); This will dispose the myWeb istance after the using block. The suggested way from ...


3

You are not disposing of the actual site, even in the same context. The rule is, if you create a web using new (or .OpenWeb() ), it must be disposed of. If you use the system object, so SPContext.Current.Web, it must not be disposed. Here is an in-depth description of how and when to dispose. So... to summarize, this is what to do with SPContext.Current: ...


3

I can see several issues here: 1. I fail to understand how come you have so many master pages, as for sure (unless you have a specific business scenario) you shouldn't have that many. In either case, do NOT modify the out-of-the-box. 2. If you by "script reference" you mean some JavaScript, there is a simpler, better way starting SharePoint 2010, and that is ...


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