Login failed when a web service tries to communicate with SharePoint 2007

Posted by tata9999 on Stack Overflow See other posts from Stack Overflow or by tata9999
Published on 2009-07-06T08:31:38Z Indexed on 2010/03/21 8:01 UTC
Read the original article Hit count: 396

Hi, I created a very simple webservice in ASP.NET 2.0 to query a list in SharePoint 2007 like this:

namespace WebService1
{
    /// <summary>
    /// Summary description for Service1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class Service1 : System.Web.Services.WebService
    {
    	[WebMethod]
    	public string HelloWorld()
    	{
    		return "Hello World";
    	}

    	[WebMethod]
    	public string ShowSPMyList()
    	{
    		string username = this.User.Identity.Name;
    		return GetList();
    	}

    	private string GetList()
    	{
    		string resutl = "";
    		SPSite siteCollection = new SPSite("http://localhost:89");
    		using (SPWeb web = siteCollection.OpenWeb())
    		{
    			SPList mylist = web.Lists["MySPList"];
    			SPQuery query = new SPQuery();
    			query.Query = "<Where><Eq><FieldRef Name=\"AssignedTo\"/><Value Type=\"Text\">Ramprasad</Value></Eq></Where>";
    			SPListItemCollection items = mylist.GetItems(query);
    			foreach (SPListItem item in items)
    			{
    				resutl = resutl + SPEncode.HtmlEncode(item["Title"].ToString());
    			}
    		}
    		return resutl;
    	}
    }
}

This web service runs well when tested using the built-in server of Visual Studio 2008. The username indicates exactly my domain account (domain\myusername).

However when I create a virtual folder to host and launch this web service (still located in the same machine with SP2007), I got the following error when invoking ShowSPMyList() method, at the line to execute OpenWeb(). These are the details of the error:

System.Data.SqlClient.SqlException: Cannot open database "WSS_Content_8887ac57951146a290ca134778ddc3f8" requested by the login. The login failed. Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.

Does anyone have any idea why this error happens? Why does the web service run fine inside Visual Studio 2008, but not when running stand-alone? I checked and in both cases, the username variable has the same value (domain\myusername).

Thank you very much.

© Stack Overflow or respective owner

Related posts about sharepoint

Related posts about sharepoint2007