Programatically download file from Teams SharePoint

%3CLINGO-SUB%20id%3D%22lingo-sub-1633834%22%20slang%3D%22en-US%22%3EProgramatically%20download%20file%20from%20Teams%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1633834%22%20slang%3D%22en-US%22%3E%3CP%3EI%20am%20migrating%20a%20project%20from%20a%20standalone%20SharePoint%20to%20one%20that%20is%20integrated%20with%20Teams.%26nbsp%3B%20My%20application%20currently%20downloads%20an%20XML%20file%20and%20may%20checkout%20and%20upload%20and%20check%20in.%26nbsp%3B%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Ethe%20current%20code%20to%20download%20does%20not%20work%20with%20the%20Teams%20SharePoint%2C%20i%20get%20403%20Forbidden%20errors.%26nbsp%3B%3C%2FP%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CPRE%20class%3D%22lia-code-sample%20language-applescript%22%3E%3CCODE%3E%20%20public%20static%20XmlDocument%20GetConfig(string%20fileUrl)%0A%20%20%7B%0A%0A%20%20%20Uri%20fileUri%20%3D%20new%20Uri(fileUrl)%3B%0A%0A%20%20%20string%20server%20%3D%20fileUri.AbsoluteUri.Replace(fileUri.AbsolutePath%2C%20%22%22)%3B%0A%20%20%20string%20serverrelative%20%3D%20fileUri.AbsolutePath%3B%0A%20%20%20XmlDocument%20doc%20%3D%20new%20XmlDocument()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20try%0A%20%20%20%20%20%20%20%20%20%20%20%20%7B%0A%20%20%20%20Microsoft.SharePoint.Client.ClientContext%20clientContext%20%3D%20new%20Microsoft.SharePoint.Client.ClientContext(server)%3B%0A%20%0A%20%20%20%20Microsoft.SharePoint.Client.FileInformation%20f%20%3D%20Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext%2C%20serverrelative)%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20clientContext.ExecuteQuery()%3B%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20doc.Load(f.Stream)%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20return%20doc%3B%0A%20%20%20%20%20%20%20%20%20%20%20%20%7D%0A%20%20%20%20%20%20%20%20%20%20%20%20catch%20%7B%20%7D%0A%0A%0A%0A%20%20%20%20%20%20%20%20%20%20%20%20return%20null%3B%0A%20%20%7D%3C%2FCODE%3E%3C%2FPRE%3E%3CP%3E%26nbsp%3B%3C%2FP%3E%3CP%3Eany%20suggestions%3F%3C%2FP%3E%3C%2FLINGO-BODY%3E%3CLINGO-LABS%20id%3D%22lingo-labs-1633834%22%20slang%3D%22en-US%22%3E%3CLINGO-LABEL%3EDeveloper%3C%2FLINGO-LABEL%3E%3CLINGO-LABEL%3EMicrosoft%20Teams%3C%2FLINGO-LABEL%3E%3C%2FLINGO-LABS%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1635966%22%20slang%3D%22en-US%22%3ERe%3A%20Programatically%20download%20file%20from%20Teams%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1635966%22%20slang%3D%22en-US%22%3ECould%20you%20please%20provide%20more%20information%20regarding%20the%20issue%20you%20are%20facing.%3C%2FLINGO-BODY%3E%3CLINGO-SUB%20id%3D%22lingo-sub-1644088%22%20slang%3D%22en-US%22%3ERe%3A%20Programatically%20download%20file%20from%20Teams%20SharePoint%3C%2FLINGO-SUB%3E%3CLINGO-BODY%20id%3D%22lingo-body-1644088%22%20slang%3D%22en-US%22%3E%3CP%3E%3CA%20href%3D%22https%3A%2F%2Ftechcommunity.microsoft.com%2Ft5%2Fuser%2Fviewprofilepage%2Fuser-id%2F781607%22%20target%3D%22_blank%22%3E%40SteveAZ395%3C%2FA%3E%2C%26nbsp%3Bcan%20you%20please%20help%20me%20understand%20which%20url%20are%20you%20passing%20in%20parameter%3F%3C%2FP%3E%0A%3CP%3E%26nbsp%3B%3C%2FP%3E%3C%2FLINGO-BODY%3E
Occasional Visitor

I am migrating a project from a standalone SharePoint to one that is integrated with Teams.  My application currently downloads an XML file and may checkout and upload and check in.  

 

the current code to download does not work with the Teams SharePoint, i get 403 Forbidden errors. 

 

		public static XmlDocument GetConfig(string fileUrl)
		{

			Uri fileUri = new Uri(fileUrl);

			string server = fileUri.AbsoluteUri.Replace(fileUri.AbsolutePath, "");
			string serverrelative = fileUri.AbsolutePath;
			XmlDocument doc = new XmlDocument();

            try
            {
				Microsoft.SharePoint.Client.ClientContext clientContext = new Microsoft.SharePoint.Client.ClientContext(server);
	
				Microsoft.SharePoint.Client.FileInformation f = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, serverrelative);

                clientContext.ExecuteQuery();

                doc.Load(f.Stream);
                return doc;
            }
            catch { }



            return null;
		}

 

any suggestions?

2 Replies
Could you please provide more information regarding the issue you are facing.

@SteveAZ395, can you please help me understand which url are you passing in parameter?