(401) Unauthorized exception while downloading file from SharePoint - sharepoint

I have generated an access token using OAuth mechanism for SharePoint Online server. I am using this token to create ClientContext using CSOM. While I am able to access all the sites, libraries, and folders seamlessly, I get error
The remote server returned an error: (401) Unauthorized.
while downloading the file from SharePoint Online. Below is the code that I am using for file download:
var clientContext = TokenHelper.GetClientContextWithAccessToken("https://adventurer.sharepoint.com/Subsite1", accessToken);
var list = clientContext.Web.Lists.GetByTitle("SubSite 1 Library 1");
string vquery = #"<View Scope='RecursiveAll'><Query><Where><Eq><FieldRef Name='UniqueId' /><Value Type='Lookup'>" + "6718053d-a785-489c-877f-5a4b88dcb2a7" + "</Value></Eq></Where></Query></View>";
CamlQuery query = new CamlQuery();
query.ViewXml = vquery;
var listItems = list.GetItems(query);
clientContext.Load(listItems, items => items.Take(1).Include(item => item.File));
clientContext.ExecuteQuery();
var fileRef = listItems[0].File.ServerRelativeUrl;
var fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, fileRef);
I don't understand the root cause of this error, as I am passing client context with right access token. I want to know if OpenBinaryDirect has a limitation to work with access tokens? If not, what is wrong with above code? Is there any other alternative that can be used to download using access token?

After trying a lot of alternatives, I have come to conclusion that OpenBinaryDirect() cannot be used with OAuth tokens. I was able to download file from SharePoint Online using two other approaches. I am posting the answers here so that it might help someone:
Approach 1 (OpenBinaryStream):
var file = clientContext.Web.GetFileByServerRelativeUrl(fileRef);
clientContext.Load(file);
clientContext.ExecuteQuery();
ClientResult<Stream> streamResult = file.OpenBinaryStream();
clientContext.ExecuteQuery();
While this approach works perfectly, OpenBinaryStream is not available in Microsoft.SharePoint.Client.dll <= v 14.0.0.0.
Approach 2 (WebClient or Other Http Requests):
string downloadUrl = HostURL + "/_api/web/getfilebyserverrelativeurl('" + fileRef + "')/$value";
WebClient client = new WebClient();
client.Headers.Add("Authorization", "Bearer " + accessToken);
client.DownloadFile(downloadUrl, filePath);
Note the download URL that I have used for WebClient. Normal file URL will not work to download files from SharePoint Online.

I am using C# and here is how I am currently retrieving documents from SharePoint Online. I am showing the user a list of their documents in a gridview, so I populate a DataTable with the documents. I am unsure of a way using an Access Token, but if you are able to use a Service Account like I am, then hopefully this helps you.
Namespaces
using Microsoft.SharePoint.Client;
using SP = Microsoft.SharePoint.Client;
Object Attributes
SecureString securePassword = new SecureString();
private string username = "";
ClientContext context = new SP.ClientContext("https://<root>.sharepoint.com/<site collection (unless root)>/<site>");
Constructor (This is how I am authenticating)
public SharePoint()
{
securePassword = convertToSecureString(System.Web.Configuration.WebConfigurationManager.AppSettings["O365PW"]);
username = System.Web.Configuration.WebConfigurationManager.AppSettings["O365UN"];
context.Credentials = new SharePointOnlineCredentials(username, securePassword);
}
Method to get documents
public DataTable GetDocuments(int changeID)
{
DataTable dt = new DataTable("ChangeDocuments");
DataRow dr = dt.NewRow();
dt.Columns.Add("Title");
dt.Columns.Add("URL");
dt.Columns.Add("ChangeID");
dt.Columns.Add("Modified");
dt.Columns.Add("ID");
// The SharePoint web at the URL.
Web web = context.Web;
// We want to retrieve the web's properties.
context.Load(web);
// We must call ExecuteQuery before enumerate list.Fields.
context.ExecuteQuery();
// Assume the web has a list named "Announcements".
SP.List oList = context.Web.Lists.GetByTitle("Name of your document library");
// This creates a CamlQuery that has a RowLimit of 100, and also specifies Scope="RecursiveAll"
// so that it grabs all list items, regardless of the folder they are in.
CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
query.ViewXml = "<View><Query><Where><Eq><FieldRef Name='ChangeID'/>" +
"<Value Type='Number'>" + changeID + "</Value></Eq></Where></Query><RowLimit>100</RowLimit></View>";
SP.ListItemCollection items = oList.GetItems(query);
// Retrieve all items in the ListItemCollection from List.GetItems(Query).
context.Load(items);
context.ExecuteQuery();
foreach (Microsoft.SharePoint.Client.ListItem listItem in items)
{
// We have all the list item data. For example, Title.
dr = dt.NewRow();
dr["Title"] = listItem["FileLeafRef"];
if (String.IsNullOrEmpty(listItem["ServerRedirectedEmbedUrl"].ToString()))
{
dr["URL"] = "<root>/<site>/<document library>" + listItem["FileLeafRef"].ToString();
}
else
{
dr["URL"] = listItem["ServerRedirectedEmbedUrl"];
}
dr["ChangeID"] = listItem["ChangeID"];
dr["Modified"] = Convert.ToDateTime(listItem["Modified"]).ToString("MMM.dd,yyyy h:mm tt");
dr["ID"] = listItem["ID"];
dt.Rows.Add(dr);
}
return dt;
}
Method to convert password to secure string
private SecureString convertToSecureString(string strPassword)
{
var secureStr = new SecureString();
if (strPassword.Length > 0)
{
foreach (var c in strPassword.ToCharArray()) secureStr.AppendChar(c);
}
return secureStr;
}

Related

C# SharePoint CSOM change file properties after upload

I have searched and found several examples of how to do this, but I can't make them work - well part of it doesn't work.
I can perform the file upload, but the following attempt to change properties fail.
I'm attempting to upload a file from a base64 payload - this part works - but when I afterwards attempt to edit the properties (custom column) associated with the file, the code fails.
Here is the code (simplified for readability):
(note that props is a collection of custom objects (FileProperty) with a name and a value attribute).
using (ClientContext context = new ClientContext("<sharepoint_server_url>"))
{
context.Credentials = new SharePointOnlineCredentials(<usr>,<secure_pwd>);
using (System.IO.MemoryStream ms = new System.IO.MemoryStream(Convert.FromBase64String(<base64_content>)))
{
File.SaveBinaryDirect(context, <relative_path>, ms, true);
}
// file is uploaded - so far so good!
// attempt to edit properties of the file.
if (props != null)
{
if (props.Count > 0)
{
File newFile = context.Web.GetFileByServerRelativeUrl(<relative_path>);
context.Load(newFile);
context.ExecuteQuery();
newFile.CheckOut();
ListItem item = newFile.ListItemAllFields;
foreach (FileProperty fp in props)
{
item[fp.name] = fp.value;
}
item.Update();
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
}
}
}
This code throws an exception in the part where I try to update the properties.
Message: The file was not found.
Can anyone tell me what is wrong with this example or provide another example on how to do this?
Also, a question - is there a way to address a file by a unique ID which is the same regardless of where in the SharePoint server the file is located or moved to?
I hope someone can help me out - thanks :)
Ok, I found a solution to my problem. I don't know why this works better, it just does.
For all I know, I'm doing the exact same thing, just in another way - maybe someone else who knows more about SharePoint than me (which isn't much) can explain why this works while the first example I posted doesn't.
Previous to the code shown, I ensure that <site_url> doesn't end with "/" and that <library_name> doesn't start or end with "/" and that <file_name> doesn't start or end with "/".
With the code below I can uplaod a file and update properties, in my case i changed "Title" and a custom column "CustCulomnA" and it workes.
using (ClientContext context = new ClientContext(<site_url>))
{
context.Credentials = new SharePointOnlineCredentials(<usr>, <secure_pwd>);
FileCreationInformation fci = new FileCreationInformation()
{
Url = <file_name>,
Content = Convert.FromBase64String(<base64_content>),
Overwrite = true
};
Web web = context.Web;
List lib = web.Lists.GetByTitle(<library_name>);
lib.RootFolder.Files.Add(fci);
context.ExecuteQuery();
response.message = "uploaded";
if (props != null)
{
if (props.Count > 0)
{
File newFile = context.Web.GetFileByUrl(<site_url> +"/"+ <library_name> + "/" + <file_name>);
context.Load(newFile);
context.ExecuteQuery();
newFile.CheckOut();
ListItem item = newFile.ListItemAllFields;
foreach (FileProperty fp in props)
{
item[fp.name] = fp.value;
}
item.Update();
newFile.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
context.ExecuteQuery();
Make sure the file server relative url is valid in this case.
For example if the complete url is:
https://zheguo.sharepoint.com/sites/test/Shared%20Documents/test.jpg
Then relative url should be
/sites/test/Shared%20Documents/test.jpg
And you can also use GetFileByUrl method, passing the complete file url like this:
clientContext.Credentials = new SharePointOnlineCredentials(userName, securePassword);
Web web = clientContext.Web;
clientContext.Load(web);
clientContext.ExecuteQuery();
File file = web.GetFileByUrl("https://zheguo.sharepoint.com/sites/test/Shared%20Documents/test.jpg");
clientContext.Load(file);
clientContext.ExecuteQuery();
file.CheckOut();
ListItem item = file.ListItemAllFields;
item["Title"] = "Test";
item.Update();
file.CheckIn(string.Empty, CheckinType.OverwriteCheckIn);
clientContext.ExecuteQuery();
}

Prevent a group of user access Shared Document using csom

I want to prevent a group of user in sharepoint (Ex: Member) having access to "Shared Documents" by using csom.
However, It doesn't work when I excute my code (The account which are in the group i mention above still have access in my site.
There is my code:
ClientContext context = new ClientContext("https://example.com/sites/Litware");
SecureString password = new SecureString();
foreach (char c in "abcd".ToCharArray()) password.AppendChar(c);
context.Credentials = new SharePointOnlineCredentials("example#example.onmicrosoft.com", password);
var web = context.Web;
context.Load(web);
context.ExecuteQuery();
var memGroup = web.SiteGroups.GetByName("Member");
context.Load(memGroup);
var users = memGroup.Users;
context.Load(users);
context.ExecuteQuery();
foreach (var user in users)
{
Principal principal = web.EnsureUser(user.LoginName);
var folder = web.GetFolderByServerRelativeUrl("/Shared Documents");
var roleDef = context.Site.RootWeb.RoleDefinitions.GetByType(RoleType.None);
var roleBindings = new RoleDefinitionBindingCollection(context) { roleDef };
folder.ListItemAllFields.BreakRoleInheritance(true, false);
folder.ListItemAllFields.RoleAssignments.Add(principal, roleBindings);
}
So where did I get it wrong and how to solve it?
Please refer the following code:
Folder folder = ctx.Web.GetFolderByServerRelativeUrl("/sites/dev/shared%20documents");
ctx.Load(folder);
ctx.Load(folder.ListItemAllFields);
ctx.ExecuteQuery();
Group groupToRemove = ctx.Web.SiteGroups.GetByName("dev Members");
ctx.Load(groupToRemove);
ctx.ExecuteQuery();
folder.ListItemAllFields.BreakRoleInheritance(true, false);
folder.ListItemAllFields.RoleAssignments.GetByPrincipal(groupToRemove).DeleteObject();
ctx.ExecuteQuery();
Reference:
Remove specific user permissions from one folder in Sharepoint online with powershell

Multiple Querys context.Load SP O365

I need to retrieve a lot of User PersonProperties, and i was wondering if it is possible to load it all together.
For example:
var userList = web.SiteUserInfoList.GetItems(CamlQuery.CreateAllItemsQuery());
clientContext.Load(userList);
clientContext.ExecuteQuery();
PeopleManager pm = new PeopleManager(clientContext);
List<PersonProperties> users = new List<PersonProperties>();
foreach (ListItem u in userList)
{
var personProperties = pm.GetPropertiesFor(u.FieldValues["Name"].ToString());
users.Add(personProperties);
}
clientContext.Load(users); //load all users[i].UserProfileProperties and PictureUrl
clientContext.ExecuteQuery();
My app is a ProviderHosted and it is for SharePoint Online 2013
Thanks for your time.
Since SharePoint CSOM supports Request Batching you could take advantage of this feature and consider the following example that demonstrates how to submit two requests to the server in order to:
retrieve site users
retrieve profile properties for site users
Example:
//1.Load site users
var siteUsers = ctx.Web.SiteUsers;
ctx.Load(siteUsers);
ctx.ExecuteQuery();
//2.Load user profile properties for site users
var pm = new PeopleManager(ctx);
var results = new Dictionary<string,PersonProperties>();
foreach (var siteUser in siteUsers)
{
var personProperties = pm.GetPropertiesFor(siteUser.LoginName);
ctx.Load(personProperties);
results.Add(siteUser.LoginName,personProperties);
}
ctx.ExecuteQuery();
Usage
foreach (var result in results)
{
var userLoginName = result.Key;
var userProperties = result.Value;
Console.WriteLine("{0} ({1})",userLoginName, userProperties.Email);
}

How Do I get a page's URL using JSOM

I am using SharePoint 2013 workflow.
I am in the Initiation form when my my users clock the Start button to start the workflow.
I am using JSOM to start the workflow but since I am on the Initiation form, I don't know the URL of the page. I do know the list (pages) and the the list id (2).
Can someone help me retrieve the list id's url using JSOM?
Thanks
Tom
How to get Page Url in Initiation Form page:
var listId = getParameterByName('List');
var itemId = getParameterByName('ID');
var ctx = new SP.ClientContext.get_current();
var web = ctx.get_web();
var list = web.get_lists().getById(listId);
var listItem = list.getItemById(itemId);
ctx.load(listItem);
ctx.executeQueryAsync(
function () {
var itemUrl = listItem.get_item('FileRef');
console.log(itemUrl);
},
function (sender, args) {
console.log(args.get_message());
}
);
,where
function getParameterByName(name) {
name = name.replace(/[\[]/, "\\[").replace(/[\]]/, "\\]");
var regex = new RegExp("[\\?&]" + name + "=([^&#]*)"),
results = regex.exec(location.search);
return results == null ? "" : decodeURIComponent(results[1].replace(/\+/g, " "));
}
is intended for retrieving parameter from query string
Source

Sending Emails in Sharepoint

I need to know what is the best practice for sending emails from my sharepoint webparts and/or customized features.
Should I just use the normal .Net classes to send email ? or is their a better way to do it through integration with an outlook server ?
Easy way is to use the built in Utilities, this will then use the mail server setttings setup in central admin
using Microsoft.SharePoint.Utilities;
SPUtility.SendEmail(SPContext.Current.Web, false, false,
"toaddress#mail.com", "subject",
"body");
Universal way to send email in any context(where SPWeb not available) is read OutboundMailService settings which is used in SPUtility. Then create SmtpClient manually:
var adminApp = SPAdministrationWebApplication.Local;
var instance = adminApp.OutboundMailServiceInstance;
var server = instance.Server.Address;
var defaultFrom = adminApp.OutboundMailSenderAddress;
var client = new SmtpClient();
client.Host = server;
message.From = new MailAddress(defaultFrom );
...
You also can use this code for dynamic mail id. this code gets the mail according to the user. I have used CAML query to get the data for the email content from the lists.
SPUser AssigUser = oWeb.EnsureUser(Assigned_Username);
SPQuery mquery = new SPQuery();
mquery.Query = #"<Where><Eq><FieldRef Name='Email_x0020_Type' />
<Value Type='Text'>Review - Ready for Review</Value>
</Eq></Where>";
string Emailsub = "";
string Emailbody = "";
SPList mList = oWeb.Lists["Email Content"];
SPListItemCollection itemcollection = mList.GetItems(mquery);
foreach (SPListItem item in itemcollection)
{
Emailsub = item["Email Subject"].ToString();
Emailbody = item["Email Content"].ToString();
SPUtility.SendEmail(oWeb, false, false, AssigUser.Email, Emailsub,
Emailbody + "</br>" + oWeb.Url);
break;
}
using overload with StringDictionary arguments (source)
StringDictionary headers = new StringDictionary();
headers.Add("to", currCtxt.Web.CurrentUser.Email);
headers.Add("cc", "xyz#abc.com");
headers.Add("bcc", "");
headers.Add("from", "email#add.com");
headers.Add("subject", "Email Subject");
headers.Add("content-type", "text/html");
string bodyText = "Hello how are you?";
SPUtility.SendEmail(currCtxt.Web, headers, bodyText.ToString());

Resources