Okilele

Hello All,

I try to access SharePoint sites using the sample explained in the WSS SDK:

SPSecurity.CodeToRunElevated elevatedGetSitesAndGroups = new SPSecurity.CodeToRunElevated(GetSitesAndGroups);
SPSecurity.RunWithElevatedPrivileges(elevatedGetSitesAndGroups);

and

protected void GetSitesAndGroups()
{
SPContext spc = SPContext.Current;

...

and get the "Operation is not valid due to the current state of the objec" error when accessing the SPContext.

My SP site address is http://myServer

The url of my webapp is http://myServer/_layouts/WebApp01

Not "trust" tag or anything about security in my web.config.

Any idea



Re: SharePoint - Development and Programming SPContext spc = SPContext.Current;

James Yarnell

WebApp01 is not a managed path in SharePoint. If you want to work with SharePoint objects you will have to create them yourself mostlikely starting with:

SPSite site = new SPSite("http://myServer");

And then going from there. If your code was executing in a managed path the SharePoint object like SPContext.Current would populate for you automatically.





Re: SharePoint - Development and Programming SPContext spc = SPContext.Current;

Okilele

Hi James,

yes, I tried SPSite site = new SPSite("http://myServer"); and this works.

But I need my code to be independant from the SPSite's url.

For sites having http://myServer or http://myServer/mySite or http://myServer/sites/myCollectionSite what should be the correct urls for the Web apps

I followed instructions in http://msdn2.microsoft.com/en-us/library/ms479423.aspx where they say :

-----------------------------------------

If you are creating a Web application, type a path [the path of the Web app in Visual Studio] like one of the following, which either creates the application in Local_Drive:\\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\LAYOUTS or in Local_Drive:\\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\ADMIN. Create applications in the administrative directory that use the Microsoft.Administration namespace to work with global settings.

http://MyServer/_layouts/MyApplicationName

http://MyAdminServerStick out tongueort#/MyApplicationName

-----------------------------------------

The real problem is this is the starting point for everything.

Anyway thanks for the "managed path" keyword, there's a lot of interesting stuff about it.





Re: SharePoint - Development and Programming SPContext spc = SPContext.Current;

James Yarnell

Based on what you want to do, I would use the Request object to get the full URL of the page and then parse that back to the _layouts directory to determine the web that the page is suppose to be operting in. Then load the SPSite and then the correct SPWeb.

Hope this helps.





Re: SharePoint - Development and Programming SPContext spc = SPContext.Current;

Okilele

You are right, the Request object may help.

But, as you were able to guess, I am in a learning phase and I need to understand why my 1st line of SharePoint code does not work, however I copied it from the SDK documentation!

Do you think this is coming from the correlation between the SharePoint url and the Web app path Have you a sample of working url/path I read somebody had the same problem when working with the SharePoint root but everything was Ok when working with a server/site/SiteCollection url ...

Or may be this comes from some(security) setting of my server