Skip to content

Commit 85dcf6d

Browse files
Merge pull request SharePoint#5397 from JarbasHorst/patch-3
Update working-with-folders-and-files-with-rest.md
2 parents 70391fb + 70735bf commit 85dcf6d

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

docs/sp-add-ins/working-with-folders-and-files-with-rest.md

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,69 @@ GET https://{site_url}/_api/web/GetFileByServerRelativeUrl('/Folder Name/{file_n
118118
Authorization: "Bearer " + accessToken
119119
```
120120

121+
The following code sample shows how to **retrieve a file when you know its URL by using the REST endpoint above and C#**.
122+
123+
```csharp
124+
/// <summary>
125+
/// Download File Via Rest API
126+
/// </summary>
127+
/// <param name="webUrl">https://xxxxx/sites/DevSite</param>
128+
/// <param name="credentials"></param>
129+
/// <param name="documentLibName">MyDocumentLibrary</param>
130+
/// <param name="fileName">test.docx</param>
131+
/// <param name="path">C:\\</param>
132+
public static void DownloadFileViaRestAPI(string webUrl, ICredentials credentials, string documentLibName, string fileName, string path)
133+
{
134+
webUrl = webUrl.EndsWith("/") ? webUrl.Substring(0, webUrl.Length - 1) : webUrl;
135+
string webRelativeUrl = null;
136+
if (webUrl.Split('/').Length > 3)
137+
{
138+
webRelativeUrl = "/" + webUrl.Split(new char[] { '/' }, 4)[3];
139+
}
140+
else
141+
{
142+
webRelativeUrl = "";
143+
}
144+
145+
using (WebClient client = new WebClient())
146+
{
147+
client.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f");
148+
client.Credentials = credentials;
149+
Uri endpointUri = new Uri(webUrl + "/_api/web/GetFileByServerRelativeUrl('" + webRelativeUrl + "/" + documentLibName + "/" + fileName + "')/$value");
150+
151+
byte[] data = client.DownloadData(endpointUri);
152+
FileStream outputStream = new FileStream(path + fileName, FileMode.OpenOrCreate | FileMode.Append, FileAccess.Write, FileShare.None);
153+
outputStream.Write(data, 0, data.Length);
154+
outputStream.Flush(true);
155+
outputStream.Close();
156+
}
157+
}
158+
159+
static void Main(string[] args)
160+
{
161+
string siteURL = "https://xxxxx/sites/DevSite";
162+
163+
//set credential of SharePoint online
164+
SecureString secureString = new SecureString();
165+
foreach (char c in "Password".ToCharArray())
166+
{
167+
secureString.AppendChar(c);
168+
}
169+
ICredentials credentials = new SharePointOnlineCredentials("xxxxxx.onmicrosoft.com", secureString);
170+
171+
//set credential of SharePoint 2013(on-premise)
172+
//string userName = "Administrator";
173+
//string password = "xxxxxxx";
174+
//string ___domain = "CONTOSO";
175+
//ICredentials credentials = new NetworkCredential(userName, password, ___domain);
176+
177+
DownloadFileViaRestAPI(siteURL, credentials, "MyDocumentLib", "test.docx", "c:\\");
178+
179+
Console.WriteLine("success");
180+
Console.ReadLine();
181+
}
182+
```
183+
121184
The following example shows how to **create a file and add it to a folder**.
122185

123186
```http

0 commit comments

Comments
 (0)