SPContext.Current is null in a web service under a sharepoint site - sharepoint

I'm exposing a web service in a SharePoint site e.g. http://server/_vti_bin/subdir/mywebservice.asmx.
In the web service method, SPContext.Current appears to be null.
(Interestingly, I'm looking at an example in Chapter 5 of Inside Windows SharePoint Services 3.0 and they use SPContext.Current inside a web service!)
Any idea what am I missing? Thanks in advance.

The only times when I saw a SPContext.Current = null was when the webservice was called at a virtual location that didn't exist (i.e. an URL that was only half correct).
In your problem specification, you mention "_vti_bin/subdir". I have not seen this subdir construct in a WebService hosted within SharePoint yet (disclaimer: my knowledge is limited to 2 or 3 working implementations of a custom webservices within SharePoint)
Normally, you'd
Put the asmx straight into the ISAPI folder of the 12 hive. This will map onto _vti_bin.
Put the assembly, if any, in the GAC (or in the website bin, but I'd suggest using the GAC until you get the basics up and running)
Do stuff for discovery if you really need to, but for testing this shouldn't be necessary
It might not be related at all, but I'd still try to get things working from the _vti_bin first, then try and mess with subfolders. SharePoint's url virtualization can be painful.

Is it possible that your subdirectory is set up as a web application and is therefore operating in a different context?

How are you calling the webservice? Is this an ajax call from javascript/jquery? If yes, have you added the ScriptService attribute on the webservice class?
[System.Web.Scripts.Services.ScriptService]
public class SimpleWebService : System.Web.Services.WebService
{
...
...
}

Related

SharePoint development nightmares

I thought I'd give SharePoint development a go, to broaden my understanding of Microsoft technologies and ran into a situation I refuse to understand.
I have a new web application created: http://localhost:11523 and set up the site collection as required. I can browse to the web site fine, without any issues, but now I want to start developing against this, using the object model.
Right, so after I struggled with SPSite site = new SPSite("http://localhost:11523"); I figured that I'm not running VS2008 in elevated permissions, so restarting VS I finally got one step further.
Whenever I step into my code, I get:
The Web application at
http://localhost:11523/ could not be
found.
I've Googled this without luck. The application is most definitely there, I can browse it, add web parts and go mad. I just can't seem to connect to it via Visual Studio.
Any ideas would be great.
EDIT
I thought I'd isolate my method, called "GetListFromSharePoint(string name)" into a test method (nUnit Framework) and to my surprise returned 9 entries from the list, as expected. When I dumped the method back into my web application (not SharePoint, trying to test the Composite Control through a normal Web site), and run into the mentioned problem.
This is in SharePoint 2007, and I'm developing on the same machine onto which SharePoint 2007 is installed.
This approach used to work, for some reason, I just can't get it to recognize SharePoint. The test stubs work find, just not the web application.
Edit 2
So there where a couple of things I "missed", which kind of solved the problem by itself.
Firstly, I was developing on a x64 Windows 2008 box, thus SharePoint 2007 was running in x64 mode. Cassini, turns out, is 32bit regardless of the platform you run it on, which caused some compiler bugs (I did not have IA64 compilers installed). After installing this, I figured out that the default web site (localhost:80) had been disabled by SharePoint.
Renabling the default web site, allowed me to create my web application as a virtual directory against it, which allowed my debugger compiler to run in x64.
My next challenge was access permissions. Because any new virtual directory on port 80 is assigned to the default application pool, is it assumed that the user does not have the right permissions, so I had to change my web application to run under my SharePoint web application's application pool.
The last thing I had to do was run my SharePoint code with elevated permissions.
Working like a charm :D
Note! Enable debug on your SharePoint web application....
Thanks,
Eric
(You have not specified version - assuming SharePoint 2010).
You need to decide what object model you want to use:
client (to be able to access the server from any other machine)
server (the one that you are trying now, can only be run locally).
Most likley reasons your code not work:
using "localhost" instead of ""computer name" in the Url. (I believe it is the reason.)
you are running your code not on the same machine as the SharePoint
you are running code under non-admin account
Check out how sites collections are configured in "Central Administration" site - urls associated with each site collection are listed there - make sure you are using correct one.
You can also try enumerating all site collections in SPWebApplication (http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.administration.spwebapplication.sites.aspx) to start expiriments.
I dont think the SPSite connect through the IIS, and if SharePoint isn't set up to respond to localhost (done in the Central Administration) you wont be able to connect to that url. IIS works a bit different here since it relays the signals to "localhost" to the "web application instance".
Start by checking in your SharePoint Central Administration. Go to "Configure alternate access mappings" in the "System Settings" section. Here you have your SharePoint instances, there are three properties which you can see directly in the list; 'Internal URL', 'Zone' and 'Public URL for Zone'.
If the Internal URL isn't set to Localhost you wont be able to use that connection you suggested. It bay be improper to change this to another url as well, so simply try to set your SPSite site = new SPSite("http://yourinternalurl:11523"); to whatever's in that box! :)
Cheers

How to access SharePoint files and folders from outside SharePoint?

I need to programatically interface with SharePoint folders, files and lists from outside SharePoint. Most tutorials focus on working within SharePoint itself, or at least on the same server where SharePoint is installed. I need to automate some tasks from completely different servers -- tasks that require reading SharePoint lists, browsing folders, checking files out and in, reading files stored in SharePoint libraries, etc. It used to be easy using UNC folder and file paths. Now many of our SharePoint sites don't allow UNC access (probably for good reasons), but my needs are the same. What languages / libraries / interfaces will allow this? I'd like to be able to do this from server-side .NET code and from PowerShell scripts (not on the SharePoint server). Thanks for any pointers.
SharePoint offers a web services API. I won't claim it's particularly friendly or fun, but it does work. You can get started learning here.
Use Sharepoint Web Services which provides a suite of standard web service endpoints you can use to do most anything you can through the objet model API.
You can use SPServices whichis a jQuery library which abstracts SharePoint's Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install.
Here's anoth example of SPServices in use Example
if you are using SharePoint 2010 you can use the Client Side Object Model (http://msdn.microsoft.com/en-us/library/ee537247(v=office.14).aspx). It will help you to access sharepoint objects, lists and everything. There are 3 types one for C#, Silverlight and Javascript.
there are more than one methods:
use csom (client side object model)
use rest services
use sharepoint out of the box web services.
If you want to be able to choose the language in which you program, I'd recommend using the Sharepoint REST API. I'm writing my service in Java, requesting data in JSON, and using Jackson to parse it into Java Objects.

Help me understand Sharepoint

We've been asked to create a web application. One part of the specification is that in future, it can be integrated into Sharepoint. The last version of this app was written in PHP and "integrated" by means of an iframe embedded into Sharepoint; not ideal.
I'm looking to understand the use of Sharepoint in this context. I believe that you can write Sharepoint Applications which are more "native" to Sharepoint than the rough-and-ready iframe approach I discussed before. How easy is it to take a standard ASP.NET MVC application and fully integrate it into Sharepoint?
Does anyone have any thoughts, experiences, or resources on this matter?
I think the first question is what kind of integration with SharePoint are you trying to accomplish? The simplest is to use the Page Viewer webpart (i.e. iframe) method. You can also write custom webparts that show data from your custom application. That's a form of integration. The ultimate form of integration, of course, is to make your application run inside of SharePoint. That leads to my next point.
SharePoint (as of version 2007) is essentially a giant ASP.NET framework. So you can theoretically use it to host any ASP.NET web application. I have actually done it before and it works. However, that was a plain old ASP.NET webforms application (not MVC). If this is what you are trying to do, you definitely would need to rewrite your php application in ASP.NET.
In Sharepoint there is a Page Viewer webpart using which you can load a different url. This way you can easily "integrate" your application to sharepoint site ;-)
But if you are really looking at Re Engineering the application in SharePoint then its a different story. You have to study the current application and then develop it in SharePoint.
This fellow has an approach to writing PHP for SharePoint. A key statement:
There are two big tricks – getting the
XML right and using NTLM
authentication.

Deploy custom web service to sharepoint server(2007/2010)?

According to MSDN, for deploying custom web service, we need to create *wsdl.aspx and *disco.aspx files, and put them with .asmx together under _vti_bin folder (C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\isapi). And put the dll under bin folder of the root of sharepoint virtual directory. It works correctly for me.
However, i also found that if i put .asmx file under the root virtual directory without creating those *wsdl.aspx and *disco.aspx files. It can work as well and much easier than the above way. So i'm wondering what's the potential issues in this way?
Without the WSDL and DISCO files, folks' web service clients/consumers may have a hard time "discovering" your service's contract. If that's not a concern for you, don't worry about it.
I have created several web services in SharePoint 2007 that were simply deployed as http://.../_layouts/mySubFolder/service.asmx. It works! For SharePoint it does not matter whether the presentation layer is HTML or XML for a web service (in both cases security must be applied, can be addressed both as http://server/_layouts/mySubFolder/service.asmx and http://server/sites/subsite/_layouts/mySubFolder/service.asmx
Things that are wrong thing about it
the word "layouts" implies "something to do with how it looks like".. which is not the case when we speak about web services
when migrating to SPS2010 where WCF services come into play, it will be different. However, I have not yet played with SPS2010 and do not yet know, how different. (Somehting about it here - http://blog.mastykarz.nl/wcf-sharepoint-context/)
Quite an old post but just thought i'd add to this as I don't believe the responses to date give the relevant detail on why SharePoint is setup this way.
The reason why you deploy webservices to ISAPI\vti_bin for SharePoint is because there are specific modifications made for requests to this folder to allow SharePoint to build an accurate discovery and WSDL file for the service based on the current virtual path of the service, not the path in the IIS site.
More specifically wsdisco.aspx and wswsdl.aspx handle requests to this virtual directory and will call the relevant disco.aspx and wsdl.aspx to generate the discovery xml or wsdl xml correctly based on the current SharePoint url.
If you add a service reference to your project for http://sharepointsite.com/sitecol/subsite/_vti_bin/lists.asmx the context for requests made on that url will be relevant to /sitecol/subsite as you would expect.
However if you add a service reference to http://sharepointsite.com/sitecol/subsite/_layouts/MyCustSvc.asmx the context will be fixed to http://sharepointsite.com/ because the default wsdl generator for ASP.NET is not aware of the SharePoint virtual paths. In which case calls to SPContext in the service will not work as expected.
There is an excellent post here with examples explaining this:
Where do you deploy custom SharePoint web service files to?

How to change SharePoint extended web application's web.config file

Using the SharePoint API, how can I modify an extended webapp web.config file?
I have to do some changes in this file to specify the connection string, membershipprovider, etc... for using Forms Authentication.
Currently, I can change the "master" webapplication web.config file, but not the extended one.
Edited
I'm using Sp 2007. I'm already using SPWebConfigModification class to modify the parent webapp. Thoses modifications are propagated to the extended webapp. I have to change ONLY the extended one. But I don't find the way to do it.
What I'm trying to achieve, is to program a little wizard that:
Shows a WebApp list so the user can select one
Extend that selected webapp to a different zone
Configure that extended zone to use Forms Authentication
Thanks
I got an answer form a blog.
In your case , you need to write a Web Application scoped feature which after you provision your site using your C# app, it is deployed to the newly-provisioned site and in the receiver of the feature you inject the required web.config settings because in that context configuration API is obtainable , but again bear the following in mind:
If you don’t use SPWebConfigModification class and either use ASP.NET 2.0 configuration API or your own mechanism , propagating changes across the farm is YOUR RESPONSIBILITY. If this happens on an standalone installation then you don;t need to be worried about this.
So it cannot be done externally. We have to do a feature in order to acheve this.
Thanks
Quick answer: look into the SPWebConfigModification class. I am assuming you're using SharePoint 2007. The best way to learn about this is to dig into open source projects:
Google code search: http://www.google.com/codesearch?q=spwebconfigmodification&hl=en&btnG=Search+Code
This project on CodePlex looks like it's one big SPWebConfigModification: http://www.codeplex.com/ajaxifymoss/Release/ProjectReleases.aspx?ReleaseId=13360

Resources