Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

I am creating a task scheduler for Windows Server 2008 to send mails for events whose date has passed and I am trying to access the task list using owssvr.dll with the following code in sharepoint 2010 to get the event date. I am unable to execute this task even if it is set to run with admin privileges.

I get the 401 unauthorized exception.

string reqUrl = url + @"/_vti_bin/owssvr.dll?Cmd=Display&List=" + ListId + "View=" + ViewId + "&XMLDATA=TRUE&noredirect=true&Query=*&FilterField1=Event_x0020_Date&FilterValue1=" + String.Format("{0:u}", DateTime.Today);
            HttpWebRequest httpwebRequest = (HttpWebRequest)HttpWebRequest.Create(reqUrl);

            HttpWebResponse httpwebResponse = (HttpWebResponse)httpwebRequest.GetResponse();
            XmlDocument doc = new XmlDocument();
            //Store Response to Stream
            Stream streamResponse = httpwebResponse.GetResponseStream();

            StreamReader streamResponseReader = new StreamReader(streamResponse);

            //Store ResponseStram As String.

            string strResponseBody = streamResponseReader.ReadToEnd();
            doc.LoadXml(strResponseBody);

I tried setting System.Net.CredentialCache.DefaultNetworkCredential and it worked in my development server. But when i changed the url property and deployed it in stage environment it gave me a 404 error. I am able to access this url in browser without any issue.

System.Net.WebException: The remote server returned an error: (404) Not Found.
share|improve this question
    
Does the user you're running as have access to the task list, or the site it's in? 'Admin' users on a system may not have access to the site itself. –  James Love Dec 20 '11 at 9:03
    
admin account has full control on the list and the normal users have contribute permission. If i assign network credential object to the web request, it works fine (even if the credentials are of a normal user) –  Shankar Dec 20 '11 at 9:56
    
You may still need to assign DefaultCredentials to the object then (which returns the current user I think). –  James Love Feb 20 '12 at 11:09
    
Were you able to resolve this issue? We are facing the similar problem. Please let us know how it was resolved. Thanks, Mubin –  user7429 Mar 20 '12 at 0:04
    
@Mubin, sorry i was not able to fix it! –  Shankar Mar 26 '12 at 6:30
add comment

2 Answers

Try :

XmlDocument  myXMLDocument = new XmlDocument();

myXMLDocument.Load(reqUrl);
share|improve this answer
    
Amit, I tried but I got the 401 Unauthorized exception! –  Shankar Dec 20 '11 at 14:57
    
Are you doing it on some button click? Please ensure that you do it via HTTP-GET request and not POST. –  Amit Kumawat Dec 21 '11 at 5:32
    
As i have mentioned in my post, this is a .exe application as a scheduled task that runs every day in the server. –  Shankar Dec 21 '11 at 7:19
add comment

I made something similar recently.

Ensure that you pass valid credentials, the url is valid and your user has the "use remote interfaces" permission (you don't need anything more except, obviously at least read access to the targeted list)

    WebRequest request = WebRequest.Create("http://{relevant SPWeb url}/_vti_bin/owssvr.dll?Cmd=Display&List=b82d7210-4730-412d-91f7-54f5ed46c699&XMLDATA=TRUE");
    listService.Credentials = new NetworkCredential("admlbar", "Epsilon$123", "domain");
    WebResponse response = request.GetResponse();
    StreamReader sr = new StreamReader(response.GetResponseStream());
    String responseAsString = sr.ReadToEnd().Trim(); // do whatever you need with the response through Linq2xml, etc.

enter image description here

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.