How do you instantiate an SPWeb object from a console application?

I am trying to write a console application that simply lists the number of lists in the sharepoint root directory.

I tried to do this using the following code, but the SPContext.Current object is null. Any ideas on how to get the web object?

 SPWeb web = SPContext.Current.Site.OpenWeb("http://localhost") ;
+3
source share
3 answers
SPSite spSite = new SPSite("http://myurl");
SPWeb spMySite = spSite.Allwebs["mysite"];
SPWeb spRootsite = spsite.RootWeb;

The console application will only run on the server, as usual. In addition, the URL http: // myurl may be the URL of the page and an SPSite object will be created. For instance. http: //myurl/mysite/pages/default.aspx will receive a valid SPSite object.

+4
source

Nat:
, SharePoint WebApp, - SPWeb SPSite.
:

using (SPSite site = new SPSite(weburl))
{
    using (SPWeb web = site.OpenWeb())
    {
        // bla bla
    }
}

: weburl SPSite, OpenWeb -.

+9

SPSite.OpenWeb(), ...

GUID SPWeb:

site.OpenWeb(webUid);

URL- - . MSDN SPSite.OpenWeb() :

site.OpenWeb(relativeUrl);
site.OpenWeb(title);

URL- , SPSite.OpenWeb():

site.OpenWeb(relativeUrl, true);
0

Source: https://habr.com/ru/post/1699546/


All Articles