function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
anchoviesanchovies 

Basic Autorization via HttpRequest to Sharepoint returns 401

Hi all,



I'm trying to send an attachment body tio sharepoint server but keep getting 401 error (not autorized). The same endpoint and credentials work fine from C# though.



Can anyone help me out here?

 

string endpoint = 'http://mysharepoint_server_url/foldername/';
string username = 'domain/username';
string password = 'password';

Http http = new Http();
HttpRequest req = new HttpRequest();

Blob headerValue = Blob.valueOf(username + ':' + password);
String authorizationHeader = 'Basic ' + EncodingUtil.base64Encode(headerValue);
eq.setHeader('Authorization', authorizationHeader);

Attachment a = [SELECT Id, Name, Body, BodyLength, ContentType from Attachment WHERE Id = 'myattachmentid' LIMIT 1];
req.setTimeout(120000);
req.setBodyAsBlob(a.Body);
req.setMethod('PUT');
string newfilepath = string.format('{0}{1}{2}.docx', new string[] {endpoint, 'SFAttachment_', a.Id});
req.setEndpoint(newfilepath);
Blob reqbody = a.Body;
req.setBodyAsBlob(reqbody);
req.setHeader('Content-Length', a.BodyLength.format());
req.setHeader('Content-Type', a.ContentType);

HTTPResponse res = http.send(req);

 

Thanks in advance

kaustav goswamikaustav goswami
Hi,

Can you check the endpoint you are hitting. Thta should contain the entire domain and it should be exposed to the internet.

Also check if the server is expecting any certificate.

Is there any specific expected format for the authorizantion header?

Thanks,
Kaustav
anchoviesanchovies

Hi Kaustav,

The endpoint is reachable from C# code from my home PC, without any certificate, so it is not the issue.

 

I'll google if there are any specific header formats required for sharepoinr 2013 though, thanks for the tip.