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


16

Change the build options in your project: Platform target: any cpu. Rebuild your project and you should be able to access it.


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

Every change that causes update of the content database for that site collection will update LastContentModifiedDate, LastSecurityModifiedDate or both. That means almost every change you have mentioned. Only changes in file system will not update the content database. That means changes in SharePoint root folder (application pages, resource files, feature ...


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


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

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


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

No, this is not supported. You need to use the web services or the Client Side Object Model (CSOM)


5

When you are outside of SharePoint farm, the Client Object Model is the only way to talk with SharePoint. https://msdn.microsoft.com/en-us/library/office/ee535451(v=office.14).aspx SPSite site = new SPSite(url); Instead of above, you can use string siteUrl = "http://MyServer/sites/MySiteCollection"; ClientContext clientContext = new ...


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

There is no need to create a new SPSite object. Instead use: properties.Web.Site


4

You are on the right track, you would need to need to open the site, check to see if the URL is to a sub-web, and then look for the list. Here is one example (from http://techpunch.wordpress.com/2009/06/03/sharepoint-2007-get-splist-object-by-url/): /// <summary> /// Gets an SPList based on the url to the list /// </summary> /// <param ...


4

Seems indeed a cache problem. Looking at the implementation of the Exists method, we can see that it internally creates a new instance of an SPSite object to check if it exists. SPSite theSite = null; try { theSite = new SPSite(uri.OriginalString); } catch (FileNotFoundException) { // do nothing, just leave the instance null. The rest of the code ...


3

Just filter your logic based on some property of your MySite like this: if ($spsite.Url -ne "http://mysite.domain.com") { ... audit code ... }


3

SPSite spsite = new SPSite(SPContext.Current.Web.Url) creates a new instance of the SPSite object, and you are responsible to dispose it. However, you don't need to dispose SPSite in SPSite spSite = SPContext.Current.Site;


3

I don't believe there is a difference in terms of what they return back (the string representation of the root URL of the site). However, in the second case you are referencing both an SPSite and SPWeb object whereas in the first you are only referencing the SPSite object (so there might be some minor performance considerations if all you are looking for is ...


3

Yes this will also work, but you are not putting an SPWeb object into SPSite object.. Instead you are just using the Url to instantiate a new SPSite object In most of the cases SPSite.Url and SPSite.RootWeb.Url return same Url.. As you have already asked it here What is difference between SPSite.Url and SPSite.RootWeb.Url


3

To build on @naim's answer, you should dispose both the Site Collections and the webs and use try/catch around the dispose or using statements. Site Collection Dispose Example from Best Practices: Using Disposable Windows SharePoint Services Objects void SPSiteCollectionForEachNoLeak() { using (SPSite siteCollectionOuter = new SPSite("http://moss")) ...


3

SPContext.Current.ListItem["ColumnName"] should do it


3

A SharePoint list lives in one site and one site only. But depending on your needs the content of the list can be displayed in other sites invarious ways. Content Query Web part Share a List View Between Sites in SharePoint 2010 implementing a simple Cross SIte collection list view webpart Page view web part showing view page (add ?IsDlg=1 to url to get ...


3

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


3

I found that it works in a console application, but when I use this piece of code with an ASP.NET application it throws IO exceptions. Since there are some 32bit dlls referenced with my ASP.NET application, this creates a problem when I try to change to platform target of 64 bit. For resolution, I created a separate service application running in a ...


3

Site Represents a collection of sites in a Web application, including a top-level website and all its subsites. Web Represents a SharePoint website. Site Template Contains a customized site design based on an existing site definition. Site templates in this context exist outside of any site definition. Real World Example The marketing ...


3

As pointed out by @Robert, it will pickup the root web. However, you don't need to dispose SPWeb object retrieved from SPSite.RootWeb property. You can also directly get the SPSite object from SPContext SPSite spsite = SPContext.Current.Site; and hence does not not need to be disposed instead of instantiating a new one which needs to be explicitly disposed. ...


2

your creating a new site that is not disposed, SPContext.Current.Web.Url only generates the string url. correct way: using (SPSite spsite = new SPSite(SPContext.Current.Web.Url)) { SPWeb spWeb = spsite.RootWeb; SPList spList = spWeb.Lists.TryGetList("MyList"); } there is no need to dispose of the spweb as you noted there is no ...


2

SPSite spsite1=new SPSite(SPContext.Current.Web.Url); will give you SPSite object. But it will be site collection object only when SPContext.Current.Web.Url is that of root web. If the web object is that of a sub site then SPSite object won't be a site collection object. The second statement SPSite spsite2=new SPSite(SPContext.Current.Site); is wrong because ...


2

from the error, its looks like you are moving a subsite not a site collection. Move-spsite only works with site collections and it Moves site collections from one content database to another. for verification you can run get-spsite "intranet/depts/research" and see if it returns an error or site id.


2

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


2

There is no buildin way to get all the lists in a site collection. Your method of looping through AllWebs and getting the Lists collection for each is the only way, but it's not efficient, so you probably want to build some cache or maintain your own list and updated it using a EventReceiver each time a list is create/delete or when a site is deleted. But ...



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