Link to home
Start Free TrialLog in
Avatar of CMAKhan
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.FileNotFoundException: 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.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
   at Microsoft.SharePoint.SPSite..ctor(String requestUrl)
   at ExpenseReportAlerter.ExpenseReportAlerter.OnElapsedTime(Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseReportAlerter.cs:line 94

If I use Run with Elevated Privileges I get this error

System.NullReferenceException: Object reference not set to an instance of an object.
   at Microsoft.SharePoint.SPSecurity.<>c__DisplayClass4.<RunWithElevatedPrivileges>b__2()
   at Microsoft.SharePoint.Utilities.SecurityContext.RunAsProcess(CodeToRunElevated secureCode)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(WaitCallback secureCode, Object param)
   at Microsoft.SharePoint.SPSecurity.RunWithElevatedPrivileges(CodeToRunElevated secureCode)
   at ExpenseReportAlerter.ExpenseReportAlerter.OnElapsedTime(Object source, ElapsedEventArgs e) in C:\Documents and Settings\vacodev\My Documents\Visual Studio 2008\Projects\ExpenseReportAlerter\ExpenseReportAlerter\mycode.cs:line 98

Can I even access Sharepoint objects in a windows service or not? I am using C#.net code
Avatar of QPR
QPR
Flag of New Zealand image

Yes you can so long as you reference the correct dlls in your code.
Where are the web services hosted? Are they on a sharepoint web front end?
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.
 
 
Avatar of CMAKhan
CMAKhan

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.
ASKER CERTIFIED SOLUTION
Avatar of CMAKhan
CMAKhan

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial