1

I have a Visual WebPart, in which I have used REST API to get the data from different lists of different project sites. Below is the code for that.

string searchURLWithParam = "http://mysp2013/_api/search/query?querytext='MyKeyword'&selectproperties='ID%2cTitle%2cPath%2cAssignedTo'";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(searchURLWithParam);
request.Method = "GET";
request.Accept = "application/atom+xml";
request.ContentType = "application/atom+xml;type=entry";

NetworkCredential cred = CredentialCache.DefaultNetworkCredentials; //new System.Net.NetworkCredential("user", "password", "domain");
request.Credentials = cred;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();

As you can see in above code, if I use CredentialCache.DefaultNetworkCredentials in Visual WebPart, it gives me error:

500, Internal Server Error.

Instead if I use commented code which is new System.Net.NetworkCredential("user", "password", "domain");, it gives me result, which I don't want to use because this credentials needs to be saved somewhere.

Can I get the data without adding user credentials in the code here? or any other alternative to fetch data with REST API in Visual WebPart?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.