I am migrating my WebParts from Farm Solution to Sandboxed solutions in SharePoint 2010.
Before I was doing this:
using (SPSite site = new SPSite(SPContext.Current.Web.Url) )
{
...
..
}
My sandbox solutions fails when accessing SPContext.Current.
How can I obtain the current SPSite or the current URL?
Thanks in advance!
Try use HttpContext.Current.Request.Url or HttpRequest.Url Property
I believe your problem is using the SPSite constructor in the Sandbox. The following should work:
SPSite siteCol = SPContext.Current.Site;
According to MSDN, try this:
using (SPSite oSiteCollection = new SPSite("http://" + System.Environment.MachineName + "/sites/Site_Name"))
{
...
}
Related
this is my code:
SPWeb oWeb = SPContext.Current.Web;
SPUserToken token = oWeb.AllUsers[#"SHAREPOINTSYSTEM"].UserToken;
using (SPSite elevatedSite = new SPSite(oWeb.Site.ID, token))
{
using (SPWeb elevatedweb = site.OpenWeb())
{
}
}
i run this success. But i see in ULS log of sharepoint, have exceptions as: "Don't dispose object web". I think when i use "using" for proccess, SPSite and SPWeb auto release memory.
Please help me in this problem
According to SPDisposeCheck rule 120, SPSite.OpenWeb should be disposed, so I don't think your error is related to elevatedweb. You might want to check other areas of your code. Definitely make sure that oWeb isn't being disposed.
Use SPDisposeCheck tool to identify if you have memory leaks in this piece of code.
http://archive.msdn.microsoft.com/SPDisposeCheck
Can you please paste the ULS log entry and how you identified it's definitely related to your code?
Your code: is it
using (SPWeb elevatedweb = site.OpenWeb()) OR
using (SPWeb elevatedweb = elevatedSite.OpenWeb()) ?
Can you please paste the complete piece of code?
I want to Change Title and No of Links in Recent Changes of Sharepoint 2010's Wiki page library.
I made one custom user control for that. It work fine but there is one problem that I am unable to pass the url of site in
using (SPSite site = new SPSite(SiteCollection))
I have to pass SiteCollection Statically. I want to make it dynamic. Mine site is a sub site. Is it possible to get Site link to open the Web..
Thanks
just use:
SPSite site = SPContext.Current.Site
the same goes for using a subsite:
SPWeb web = SPContext.Current.Web
No need for using statements if you use the context, you dont need to dispose it :)
Try using this : SPContext.Current.Web.Site.Url.
Nipesh,
You can getting all Webs using below code
SPSite rootSite = SPContext.Current.Site;
foreach (SPWeb oWeb in rootSite.AllWebs)
{
//This Using for SPSite is optional
using (SPSite site = new SPSite(oWeb.Url))
{
//This Using for SPWeb is optional
using (SPWeb web = site.OpenWeb())
{
//You can get all web in this object & perform operation
}
}
oWeb.Dispose();
}
Above both Using for SPSite & SPWeb are optional you can direct access SPWeb object from oWeb object.
Happy Coding...!!!
I'm hoping someone can help me out. I need to get the root web of the current site from the SPContext. It's easily done with the following
SPContext.Current.Site.RootWeb
I'm comfortable with the idea that the SPSite object here at SPContext.Current.Site.RootWeb shouldn't be disposed of, but what about the SPWeb object I'm getting from the SPSite. Will, when the SPSite get's disposed, the rootweb SPWeb get disposed to? Or do I need to dispose of it myself?
Calls to SPSite.RootWeb should not be disposed. Disposing the SPSite will also dispose the RootWeb.
There is a bug in SPDisposeCheck where it flags if you do dispose it, and if you don't (damned either way!) I detailed how I solved this in this blog post, as you can't use an SPDisposeCheckIgnore attribute in elevated privileges blocks.
No you should not. You should only dispose objects you are in control of. Because the context is something created by SharePoint you do not dispose of this as other objects may be dependent upon this.
If you were to create your own instance of an SPWeb from this objects properties than it would need to be disposed. I.e..
using (SPSite site = new SPSite(SPContext.Current.Site.RootWeb.Url))
using (SPWeb web = site.OpenWeb()) {
// do something
}
Here is an article on the best practices of disposing SharePoint objects.
http://msdn.microsoft.com/en-us/library/aa973248(v=office.12).aspx
You should normally use SPSite and SPWeb in a using clause.
using (SPSite site = new SPSite("http://mysite.sharepoint.com"))
{
using (SPWeb web = site.OpenWeb())
{
// TODO: code for using SPWeb object
}
}
This automatically will correctly release the SPWeb object after you are done with it.
I am trying to write a console app that simply lists the number of lists at the sharepoint root.
I tried doing it by using the following code, but the object SPContext.Current is null. Any ideas of how to get the web object?
SPWeb web = SPContext.Current.Site.OpenWeb("http://localhost") ;
Just adding a little thing to Nat's post:
Even if it's not a important as in a SharePoint WebApp, it's still recommenced to dispose all SPWeb and SPSite objets.
So do keep good habits:
using (SPSite site = new SPSite(weburl))
{
using (SPWeb web = site.OpenWeb())
{
// bla bla
}
}
Note: you can directly pass the weburl to SPSite constructor, so OpenWeb will open the given web.
SPSite spSite = new SPSite("http://myurl");
SPWeb spMySite = spSite.Allwebs["mysite"];
SPWeb spRootsite = spsite.RootWeb;
The console app will only run on the server as usual.
Also, the url used http://myurl can be a url to a page and an SPSite object will be created. E.g. http://myurl/mysite/pages/default.aspx will get a valid SPSite object.
There are a couple of other ways you can use SPSite.OpenWeb() as well...
If you keep track of the SPWeb object's GUID:
site.OpenWeb(webUid);
Using a web's server or site relative URL or title, see MSDN SPSite.OpenWeb(string) for more details:
site.OpenWeb(relativeUrl);
site.OpenWeb(title);
Using the precise relative URL and avoiding any clever stuff that SPSite.OpenWeb(string) uses:
site.OpenWeb(relativeUrl, true);
Hi I am using the SharePoint namespace to pull items from various lists throughout the site. My web part works, but only on my account. When I try it on another account it gives me "Error: Access Denied" for the page. I have taken all web parts out and have only this web part on the page. When I remove the following lines the page loads for everyone, when I add it back in however it does not work. I am guessing this is some permission problem. I was wondering is there away to programatically query different lists on SharePoint by assigning a user id to use? Thank you for any help
...
SPSite site = new SPSite(_SPSite);
SPWeb eachWeb = site.AllWebs[0];
SPListItemCollection myItemCollection = eachWeb.Lists["Listings"].Items;
...
You're correct, the access denied error is occurring when you're using an account which does not have access to the "Listings" list in the current website.
The easiest way around the issue is to use a SPSecurity.RunWithElevatedPrivleges call:
SPSecurity.RunWithElevatedPrivleges(delegate()
{
//Your code here
});
which will run whatever code is contained in the anonymous method using the SharePoint/System account, granting complete control. Be careful when using this technique though, as it equivalent to running code at full trust with a super user account. There are other caveats to be aware of as well.
Try:
SPWeb eachWeb = SPContext.Current.Site.RootWeb.Webs[0];
SPListItemCollection myItemCollection = eachWeb.Lists["Listings"].Items;
Remember that SPWeb should be used in a using block, or disposed of explicitly after use.
As regards the first caveat from EvilGoatBob, I quote:
"If you're manipulating any Object Model elements within your elevated method, you need to get a fresh SPSite reference inside this call. For example
SPSecurity.RunWithElevatedPrivileges(delegate(){
SPSite mySite = new SPSite(http://sharepoint/);
SPWeb myWeb = SPSite.OpenWeb();
// further implementation omitted
});"
Notice that the site parameter is hard-coded - this is because of a bug. If you instead had tried:
using (SPSite site = new SPSite("http://" + System.Environment.MachineName)) {}
You would get the rather generic "No SharePoint Site exists at the specified URL..." error. This caused me no end of grief. Bottom line is that you have to hard-code the server name (unless anyone has an alternative). You can also get a similar error message when debugging Web Parts for the first time with VSeWSS 1.3.
You do not need to hardcode the server name in this case because your requirement is to retrieve items from list inside the same site as your webpart. You are correct, if you do not have enough privileges with your account, then you get the Access Denied. The solution is to create a new SPSite object within a different security context, and do your work:
SPSecurity.RunWithElevatedPrivileges(delegate()
{
using (SPSite site = new SPSite(SPContext.Current.Site.Url))
{
using (SPWeb web = site.OpenWeb())
{
//the web object was retrieved with elevated privileges under the system account.
//do your work here:
SPListItemCollection myItemCollection = web.Lists["Listings"].Items;
//...
}
}
}
);
With the code above, your webpart is portable because there's no hardcoding, and runs in the correct security context while disposing of all unmanaged SPRequest objects created by the SPSite and SPWeb constructors.