Hot answers tagged spsite
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 ...
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()
...
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 ...
3
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
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
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 = ...
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
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
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 ...
1
Are these sites in the same Farm? The code you have above will only be able to access SPSite object that runs on the same Farm as where you are in now. In other words, you cannot Access sites from a different farm.
If you really need to access some data, then you will have to call the Web Services on "http://site2"
1
You can specify a filter while retrieving the Site collection list from Get-SPSite:
$spsites = Get-SPSite -Filter { $_.Url -ne "http://mysite.com" }
The Filter parameter is described here.
1
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);
...
1
you can use WebsInfo property of SPWebCollection class
var list = SPContext.Current.Site.AllWebs.WebsInfo;
then you can perform action with SPWebInfo class.
1
Sorry, I can't answer all your questions, I am not good in Threads and Threadpools. But what I could suggest you, if you want a really asynchronous load of the five webparts, use javascript ajax calls of the listdata.svc service.
This way you could retrieve all your data simultaneously and after that even use jQuery to manipulate the DOM and render the ...
1
The problem with the pattern you've developed to
getSelectedList(string ListTitle) and
protected SPListItemCollection getAllCorrListItemsforCT(string myCTName)
isn't that you forgot to dispose an SPWeb object (of course you should fix the myWeb.Dispose() issue as suggested above).
The problem is that you return SPList and SPListItemCollection from your ...
1
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 ...
1
Install and run SPDisposeChecker on your compiled dll or exe. It should suggest line items in your code to analyse.
Also, please review Microsoft's excellent article on Best Practices: Using Disposable Windows SharePoint Services Objects
1
When you create something in a using statement, it gets disposed afterwards.
using(myWeb) {...}
myWeb.Dispose();
You should take off the using statement, and also check to see if the myWeb is disposable.
using (SPWeb myWeb = mySite.OpenWeb())
This instance of use is correct and will auto dispose myWeb.
1
If you are worried about disposing all the objects correctly, you can view ULS log and look for the following message
An SPRequest object was not disposed before the end of this thread. To avoid wasting system resources, dispose of this object or its parent (such as an SPSite or SPWeb) as soon as you are done using it.
Having those messages in your ...
1
All the answers which suggest logging in through the Admin account for development pose a serious risk because that is a very bad practice. You will rarely see a production environment which has been configured this way.
I will suggest that you give only the required privileges to the user with whom you are going to deploy your solutions.
1
explain what you mean by "on other system sharepoint site" please? As i understand it you created a console application that takes site as argument?
The console application must always be run on the local server itself (unless you use SharePoint 2010 and use client object model). Also the user needs to have the permissions required to run the code (eg. ...
1
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 ...
1
I ended up doing the below:
$site = new-Object Microsoft.SharePoint.SPSite("http://fake.test.com")
$web = $site.OpenWeb("/en-us/testsite")
foreach ($subweb in $web.Webs)
{
#do checking if publishing page / if checked out / add webpart / checkin and publish
#dispose subweb
}
#dispose web && site
This allows me to target the site I was aiming for, ...
1
You can start from the SPWebApplication
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint")
[system.reflection.assembly]::loadwithpartialname("microsoft.sharepoint.publishing")
$wa = [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup("http://test.fake.com")
foreach ($site in $wa.Sites) {
foreach ($web in $site.AllWebs) {
...
1
Is it possible information is being omitted by the analysis job?
Ref: SPSite.UsageInfo.Hits
The usage analysis code tracks only data recorded in the HTTP logs.
The HTTP logs record most operations except requests for files in the
_layouts directory and HTTP requests with results greater than or equal to 300 bytes.
Most SharePoint pages are > 300 ...
Only top voted, non community-wiki answers of a minimum length are eligible
