CMAKhan
asked on
Accessing Sharepoint Object Model from a Windows Service is giving error
I have to write a windows service which gathers some information from sharepoint and sends appropriate emails. I don't want to write work flow for this. I am unable to access any sharepoint component from my windows service. The same code runs fine in a console application but is failing in the windows service. for e.g
SPSite ST = new SPSite("http://test.com");
gives me the error
System.IO.FileNotFoundExce ption: The Web application at http://test.com could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.
at Microsoft.SharePoint.SPSit e..ctor(SP Farm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
at Microsoft.SharePoint.SPSit e..ctor(St ring requestUrl)
at ExpenseReportAlerter.Expen seReportAl erter.OnEl apsedTime( Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseRepor tAlerter.c s:line 94
If I use Run with Elevated Privileges I get this error
System.NullReferenceExcept ion: Object reference not set to an instance of an object.
at Microsoft.SharePoint.SPSec urity.<>c_ _DisplayCl ass4.<RunW ithElevate dPrivilege s>b__2()
at Microsoft.SharePoint.Utili ties.Secur ityContext .RunAsProc ess(CodeTo RunElevate d secureCode)
at Microsoft.SharePoint.SPSec urity.RunW ithElevate dPrivilege s(WaitCall back secureCode, Object param)
at Microsoft.SharePoint.SPSec urity.RunW ithElevate dPrivilege s(CodeToRu nElevated secureCode)
at ExpenseReportAlerter.Expen seReportAl erter.OnEl apsedTime( Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseRepor tAlerter\E xpenseRepo rtAlerter\ mycode.cs: line 98
Can I even access Sharepoint objects in a windows service or not? I am using C#.net code
SPSite ST = new SPSite("http://test.com");
gives me the error
System.IO.FileNotFoundExce
at Microsoft.SharePoint.SPSit
at Microsoft.SharePoint.SPSit
at ExpenseReportAlerter.Expen
If I use Run with Elevated Privileges I get this error
System.NullReferenceExcept
at Microsoft.SharePoint.SPSec
at Microsoft.SharePoint.Utili
at Microsoft.SharePoint.SPSec
at Microsoft.SharePoint.SPSec
at ExpenseReportAlerter.Expen
Can I even access Sharepoint objects in a windows service or not? I am using C#.net code
Meant to say that you also need to add a web reference in your code to point to the server where the servics are
Object model works only from the same server where SharePoint is installed. Yes you can compile code on non SharePoint machine as long as you have DLL references but can run code on those.
ASKER
QPR, I am not using web services any where. Sorry if my earlier post was not clear. I want to write a Windows Service which access Sharepoint data using the Object model.
mo1one, the code is residing on the server machine and the .dll's are properly referenced. the same code runs fine in a console application but breaks when I put it in a windowsservice.
mo1one, the code is residing on the server machine and the .dll's are properly referenced. the same code runs fine in a console application but breaks when I put it in a windowsservice.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Where are the web services hosted? Are they on a sharepoint web front end?