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

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

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


7

The actual point is: there's no need at all to use SPSecurity.RunWithElevatedPrivileges in a job. A job runs in OWSTIMER.exe, which uses the "SPFarm" account. "SPFarm" account has all privileges against all content DBs; it's seen as "system account" everywhere. So there's really no need to elevate its privileges at all. However, as a side note, code ...


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


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)


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

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

Title / Name: The display name of the site collection. It MUST be less than or equal to 255 characters. If nothing is specified, the site will have a default title of "Team Site". Check MSDN for other parameters. http://msdn.microsoft.com/en-us/library/dd774086(v=office.12).aspx If you look on Content Database's table, you will see the below stuff. ...


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

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

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


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

RunWithElevatedPrivileges elevates to the AppPool account of the particular WebApp which might not have permissions to change the SPSite because it is not a Site Collection owner/admin. From http://technet.microsoft.com/en-us/library/hh344224%28v=office.15%29.aspx Using highly privileged accounts for application pools or services poses a security risk ...


2

Check Get-SPSite documentation: The Filter parameter is a server-side filter for certain site collection properties that are stored in the content database; without the Filter parameter, filtering on these properties is a slow process. These site collection properties are Owner, SecondaryOwner, and LockState. The Filter parameter is a script ...



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