Tag Info

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


12

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


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.


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


6

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


5

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


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

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


4

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


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

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


3

I would strongly recommend you go through this reference article if you haven't done so already http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx#sharepointobjmodel__spwebobjects


3

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


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

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

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

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


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


2

This MSDN article Security Best Practices for Developers in SharePoint 2010 recommends against constructing SPSite objects using a Guid. Construct SPSite Objects Safely The Microsoft.SharePoint.SPSite constructor is prone to the following two problems: New SPSite objects can be constructed by using a fully qualified domain name, for ...


2

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


2

You need to use the Client Framework to instantiate an SPSite on another farm. The reason being, is that when you instantiate SPSite, the server running the code looks into it's own farm configuration to find out what content database to get the data from. If you're on FarmA and want to instantiate a site collection that exists in FarmB, you will get a ...


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


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

Assuming there isn't a method somewhere within the SharePoint object model that does this in some super efficient way (yeah, I couldn't find one either), I've done some testing that seems to suggest that the best thing to do is to access the relevant SPWebApplication object and enumerate that object's Sites property. I created a quick test harness in the ...


2

It's not a cache problem. In 2010 they implemented a timer job to do the actual site collection deletion called Gradual Site Deletion to help manage database locks when deleting large site collections. The actual deletion usually occurs very quickly, but it can take up to a few minutes.


2

I had this same issue and tried a HTTP request & check for results method (example here) as a fix, it works but was somewhat slow for checking a large number of sites at once. I ended up using the invalidate sites cache function as above like this: public bool SPSiteReallyExists(string url) { SPSite.InvalidateCacheEntry(new Uri(url), Guid.Empty); ...


2

Check the Build Target Platform. It should be "Any CPU", not 32 bit. This is checked by right click on your project, select "Properties" Then under Build, resp. Debug, Set Platform to Active(Any CPU)



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