0

How to access the SharePoint 2007 and 2013 portal with specified credential

  • Specify the credential via code (username, password)
  • Then open the site collection with that account

Currently we use below code; with assumption that the user will be the logged in user. SPSite.Open("URL") ; this should be changed to run the code in behave of different user domain.

Scenario: I am in Server A and want to access the farm in Server B with different domains.

0

To access SharePoint 2013 Portal you can use the below code.

//Get Request Digest
HttpWebRequestendpointRequest = (HttpWebRequest)HttpWebRequest.Create("<<site
url>>/_api/contextinfo");
endpointRequest.Method = "POST";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.ContentType = "application/x-www-form-urlencoded";
endpointRequest.ContentLength = 0;
NetworkCredential cred = newSystem.Net.NetworkCredential("username", "password", "domain");
endpointRequest.Credentials = cred;
HttpWebResponseendpointResponse = (HttpWebResponse)endpointRequest.GetResponse();
//Sample Post Command
HttpWebRequestendpointRequest = (HttpWebRequest)HttpWebRequest.Create("<<REST URL>>");
endpointRequest.Method = "POST";
endpointRequest.Accept = "application/json;odata=verbose";
endpointRequest.ContentType = "application/json;odata=verbose";
NetworkCredential cred = newSystem.Net.NetworkCredential("username", "password", "domain");
endpointRequest.Credentials = cred;
String username = "username";
String password = "password";
StringuserPassword = username + ":" + password;
byte[] byteArrayUP = Encoding.UTF8.GetBytes(userPassword);
endpointRequest.Headers.Add("Authorization", "Bearer" + byteArrayUP);
stringpostData = "{ '__metadata': { 'type': 'SP.Data.<<ListTitle>>ListItem' }, 'Column1':
'Text1','Column2':'Text2'}";
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// endpointRequest.ContentLength = byteArray.Length;
endpointRequest.Headers.Add("X-RequestDigest", digest);
// Get the request stream.
StreamdataStream = endpointRequest.GetRequestStream();
// Write the data to the request stream.
dataStream.Write(byteArray, 0, byteArray.Length);
// Close the Stream object.
dataStream.Close();
HttpWebResponseendpointResponse = (HttpWebResponse)endpointRequest.GetResponse();

https://www.dsouzajoel.com/2015/12/09/connect-to-sharepoint-on-premise-using-external-applications/

Your Answer

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

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