Home

Copying files across site collections NOT working with app credentials, Any Idea?

%3CLINGO-SUB%20id%3D%22lingo-sub-55414%22%20slang%3D%22en-US%22%3ECopying%20files%20across%20site%20collections%20NOT%20working%20with%20app%20credentials%2C%20Any%20Idea%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55414%22%20slang%3D%22en-US%22%3E%3CP%3EBelow%20code%20is%20working%20with%20user%20credentials%20for%20moving%20files%20across%20site%20collections.%20But%20when%20the%20user%20credentials%20are%20replaced%20with%20app%20credentials%20(ClientID%2CClientSecret)%2C%20it%20is%20throwing%3CSTRONG%3E%20unauthorized%20401%20access%3C%2FSTRONG%3E.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3ETried%20giving%20FullControl%20access%20at%20sitecollection%20and%20site%20level.%20No%20luck.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3EPlease%20provide%20your%20input%2C%20if%20anybody%20has%26nbsp%3Bsolved%20this%20issue.%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%3Epublic%20static%20void%20CopyDocuments()%0A%7B%0A%20var%20login%20%3D%20%22%26lt%3Buser%40tenant%26gt%3B.onmicrosoft.com%22%3B%0A%20var%20password%20%3D%20%22%26lt%3Bpassword%26gt%3B%22%3B%0A%20string%20srcUrl%3D%22https%3A%2F%2Fxxx.sharepoint.com%2Fsites%2Fsite1%22%3B%0A%20string%20destUrl%3D%22https%3A%2F%2Fxxx.sharepoint.com%2Fsites%2Fsite2%22%3B%0A%20string%20srcLibrary%3D%22library1%22%3B%20%0A%20string%20destLibrary%3D%22library1%22%3B%0A%20%0A%20var%20securePassword%20%3D%20new%20SecureString()%3B%0A%20foreach%20(char%20c%20in%20password)%0A%20%7B%0A%20%20securePassword.AppendChar(c)%3B%0A%20%7D%0A%0A%20var%20onlineCredentials%20%3D%20new%20SharePointOnlineCredentials(login%2C%20securePassword)%3B%0A%0A%20%2F%2F%20set%20up%20the%20src%20client%0A%20SP.ClientContext%20srcContext%20%3D%20new%20SP.ClientContext(srcUrl)%3B%0A%20srcContext.Credentials%20%3D%20onlineCredentials%3B%0A%0A%20%2F%2F%20set%20up%20the%20destination%20context%20(in%20your%20case%20there%20is%20no%20needs%20to%20create%20a%20new%20context%2C%20because%20it%20would%20be%20the%20same%20library!!!!)%0A%20SP.ClientContext%20destContext%20%3D%20new%20SP.ClientContext(destUrl)%3B%0A%20destContext.Credentials%20%3D%20onlineCredentials%3B%0A%0A%0A%20%2F%2F%20get%20the%20list%20and%20items%0A%20SP.Web%20srcWeb%20%3D%20srcContext.Web%3B%0A%20SP.List%20srcList%20%3D%20srcWeb.Lists.GetByTitle(srcLibrary)%3B%0A%20SP.ListItemCollection%20col%20%3D%20srcList.GetItems(new%20SP.CamlQuery())%3B%0A%20srcContext.Load(col)%3B%0A%20srcContext.ExecuteQuery()%3B%0A%0A%20%2F%2F%20get%20the%20new%20list%0A%20SP.Web%20destWeb%20%3D%20destContext.Web%3B%0A%20destContext.Load(destWeb)%3B%0A%20destContext.ExecuteQuery()%3B%0A%0A%20foreach%20(var%20doc%20in%20col)%0A%20%7B%0A%20%20try%0A%20%20%7B%0A%20%20%20if%20(doc.FileSystemObjectType%20%3D%3D%20SP.FileSystemObjectType.File)%0A%20%20%20%7B%0A%20%20%20%20%2F%2F%20get%20the%20file%0A%20%20%20%20SP.File%20f%20%3D%20doc.File%3B%0A%20%20%20%20srcContext.Load(f)%3B%0A%20%20%20%20srcContext.ExecuteQuery()%3B%0A%0A%20%20%20%20%2F%2F%20build%20new%20location%20url%0A%20%20%20%20string%20nLocation%20%3D%20destWeb.ServerRelativeUrl.TrimEnd('%2F')%20%2B%20%22%2F%22%20%2B%20destLibrary.Replace(%22%20%22%2C%20%22%22)%20%2B%20%22%2F%22%20%2B%20f.Name%3B%0A%0A%20%20%20%20%2F%2F%20read%20the%20file%2C%20copy%20the%20content%20to%20new%20file%20at%20new%20location%0A%20%20%20%20SP.FileInformation%20fileInfo%20%3D%20SP.File.OpenBinaryDirect(srcContext%2C%20f.ServerRelativeUrl)%3B%0A%20%20%20%20SP.File.SaveBinaryDirect(destContext%2C%20nLocation%2C%20fileInfo.Stream%2C%20true)%3B%0A%20%20%20%7D%20%0A%0A%20%20%20if%20(doc.FileSystemObjectType%20%3D%3D%20SP.FileSystemObjectType.Folder)%0A%20%20%20%7B%0A%20%20%20%20%2F%2F%20load%20the%20folder%0A%20%20%20%20srcContext.Load(doc)%3B%0A%20%20%20%20srcContext.ExecuteQuery()%3B%0A%0A%20%20%20%20%2F%2F%20get%20the%20folder%20data%2C%20get%20the%20file%20collection%20in%20the%20folder%0A%20%20%20%20SP.Folder%20folder%20%3D%20srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues%5B%22FileRef%22%5D.ToString())%3B%0A%20%20%20%20SP.FileCollection%20fileCol%20%3D%20folder.Files%3B%0A%0A%20%20%20%20%2F%2F%20load%20everyting%20so%20we%20can%20access%20it%0A%20%20%20%20srcContext.Load(folder)%3B%0A%20%20%20%20srcContext.Load(fileCol)%3B%0A%20%20%20%20srcContext.ExecuteQuery()%3B%0A%0A%20%20%20%20foreach%20(SP.File%20f%20in%20fileCol)%0A%20%20%20%20%7B%0A%20%20%20%20%20%2F%2F%20load%20the%20file%0A%20%20%20%20%20srcContext.Load(f)%3B%0A%20%20%20%20%20srcContext.ExecuteQuery()%3B%0A%0A%20%20%20%20%20string%5B%5D%20parts%20%3D%20null%3B%0A%20%20%20%20%20string%20id%20%3D%20null%3B%0A%0A%20%20%20%20%20if%20(srcLibrary%20%3D%3D%20%22My%20Files%22)%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20%2F%2F%20these%20are%20doc%20sets%0A%20%20%20%20%20%20parts%20%3D%20f.ServerRelativeUrl.Split('%2F')%3B%0A%20%20%20%20%20%20id%20%3D%20parts%5Bparts.Length%20-%202%5D%3B%0A%20%20%20%20%20%7D%0A%20%20%20%20%20else%0A%20%20%20%20%20%7B%0A%20%20%20%20%20%20id%20%3D%20folder.Name%3B%0A%20%20%20%20%20%7D%0A%0A%20%20%20%20%20%2F%2F%20build%20new%20location%20url%0A%20%20%20%20%20string%20nLocation%20%3D%20destWeb.ServerRelativeUrl.TrimEnd('%2F')%20%2B%20%22%2F%22%20%2B%20destLibrary.Replace(%22%20%22%2C%20%22%22)%20%2B%20%22%2F%22%20%2B%20id%20%2B%20%22%2F%22%20%2B%20f.Name%3B%0A%0A%20%20%20%20%20%2F%2F%20read%20the%20file%2C%20copy%20the%20content%20to%20new%20file%20at%20new%20location%0A%20%20%20%20%20SP.FileInformation%20fileInfo%20%3D%20SP.File.OpenBinaryDirect(srcContext%2C%20f.ServerRelativeUrl)%3B%0A%20%20%20%20%20SP.File.SaveBinaryDirect(destContext%2C%20nLocation%2C%20fileInfo.Stream%2C%20true)%3B%0A%20%20%20%20%7D%0A%20%20%20%7D%0A%20%20%7D%0A%20%20catch%20(Exception%20ex)%0A%20%20%7B%0A%20%20%20Log(%22File%20Error%20%3D%20%22%20%2B%20ex.ToString())%3B%0A%20%20%7D%0A%20%7D%0A%7D%3C%2FPRE%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-55414%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3ECode%20Samples%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMSDN%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EOffice%20365%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3ESharePoint%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-56541%22%20slang%3D%22en-US%22%3ERe%3A%20Copying%20files%20across%20site%20collections%20NOT%20working%20with%20app%20credentials%2C%20Any%20Idea%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-56541%22%20slang%3D%22en-US%22%3E%3Ca%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F71820%22%3E%40Juan%3C%2Fa%3E%20-%20I%20tried%20this.%20No%20luck.%20Still%20getting%20unauthorized%20permission%20access.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-55975%22%20slang%3D%22en-US%22%3ERe%3A%20Copying%20files%20across%20site%20collections%20NOT%20working%20with%20app%20credentials%2C%20Any%20Idea%3F%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-55975%22%20slang%3D%22en-US%22%3EMmm...have%20you%20try%20to%20use%20tenant%20permissions%3A%20%3CA%20href%3D%22https%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fpnp_articles%2Fdevelopment-experience-tenant-apponly-permissions-in-sharepoint-online%22%20target%3D%22_blank%22%20rel%3D%22noopener%20noreferrer%20noopener%20noreferrer%22%3Ehttps%3A%2F%2Fmsdn.microsoft.com%2Fen-us%2Fpnp_articles%2Fdevelopment-experience-tenant-apponly-permissions-in-sharepoint-online%3C%2FA%3E%3C%2FLINGO-BODY%3E
Srinivas Narula
Contributor

Below code is working with user credentials for moving files across site collections. But when the user credentials are replaced with app credentials (ClientID,ClientSecret), it is throwing unauthorized 401 access.

 

Tried giving FullControl access at sitecollection and site level. No luck.

 

Please provide your input, if anybody has solved this issue.

 

public static void CopyDocuments()
{
	var login = "<user@tenant>.onmicrosoft.com";
	var password = "<password>";
	string srcUrl="https://xxx.sharepoint.com/sites/site1";
	string destUrl="https://xxx.sharepoint.com/sites/site2";
	string srcLibrary="library1"; 
	string destLibrary="library1";
	
	var securePassword = new SecureString();
	foreach (char c in password)
	{
		securePassword.AppendChar(c);
	}

	var onlineCredentials = new SharePointOnlineCredentials(login, securePassword);

	// set up the src client
	SP.ClientContext srcContext = new SP.ClientContext(srcUrl);
	srcContext.Credentials = onlineCredentials;

	// set up the destination context (in your case there is no needs to create a new context, because it would be the same library!!!!)
	SP.ClientContext destContext = new SP.ClientContext(destUrl);
	destContext.Credentials = onlineCredentials;


	// get the list and items
	SP.Web srcWeb = srcContext.Web;
	SP.List srcList = srcWeb.Lists.GetByTitle(srcLibrary);
	SP.ListItemCollection col = srcList.GetItems(new SP.CamlQuery());
	srcContext.Load(col);
	srcContext.ExecuteQuery();

	// get the new list
	SP.Web destWeb = destContext.Web;
	destContext.Load(destWeb);
	destContext.ExecuteQuery();

	foreach (var doc in col)
	{
		try
		{
			if (doc.FileSystemObjectType == SP.FileSystemObjectType.File)
			{
				// get the file
				SP.File f = doc.File;
				srcContext.Load(f);
				srcContext.ExecuteQuery();

				// build new location url
				string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + f.Name;

				// read the file, copy the content to new file at new location
				SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
				SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
			} 

			if (doc.FileSystemObjectType == SP.FileSystemObjectType.Folder)
			{
				// load the folder
				srcContext.Load(doc);
				srcContext.ExecuteQuery();

				// get the folder data, get the file collection in the folder
				SP.Folder folder = srcWeb.GetFolderByServerRelativeUrl(doc.FieldValues["FileRef"].ToString());
				SP.FileCollection fileCol = folder.Files;

				// load everyting so we can access it
				srcContext.Load(folder);
				srcContext.Load(fileCol);
				srcContext.ExecuteQuery();

				foreach (SP.File f in fileCol)
				{
					// load the file
					srcContext.Load(f);
					srcContext.ExecuteQuery();

					string[] parts = null;
					string id = null;

					if (srcLibrary == "My Files")
					{
						// these are doc sets
						parts = f.ServerRelativeUrl.Split('/');
						id = parts[parts.Length - 2];
					}
					else
					{
						id = folder.Name;
					}

					// build new location url
					string nLocation = destWeb.ServerRelativeUrl.TrimEnd('/') + "/" + destLibrary.Replace(" ", "") + "/" + id + "/" + f.Name;

					// read the file, copy the content to new file at new location
					SP.FileInformation fileInfo = SP.File.OpenBinaryDirect(srcContext, f.ServerRelativeUrl);
					SP.File.SaveBinaryDirect(destContext, nLocation, fileInfo.Stream, true);
				}
			}
		}
		catch (Exception ex)
		{
			Log("File Error = " + ex.ToString());
		}
	}
}
2 Replies
@Juan - I tried this. No luck. Still getting unauthorized permission access.
Related Conversations
Tabs and Dark Mode
cjc2112 in Discussions on
20 Replies
Stable version of Edge insider browser
HotCakeX in Discussions on
35 Replies
flashing a white screen while open new tab
cntvertex in Discussions on
11 Replies
How to Prevent Teams from Auto-Launch
chenrylee in Microsoft Teams on
28 Replies