[英]Access to sharepoint 2013 site from web api
I'm working on a communication between a MVC web app developed in c# and typescript and a remote sharepoint site. 我正在使用C#和Typescript开发的MVC Web应用程序与远程共享点站点之间进行通信。 I want to execute crud operations for an excel file.
我想对Excel文件执行Crud操作。
I'm able to read site properties in this way: 我可以通过以下方式读取网站属性:
public async Task<string> getWebTitle(string webUrl, string usr, string psw)
{
//Creating Password
const string PWD = psw;
const string USER = usr;
const string RESTURL = "{0}/_api/web?$select=Title";
//Creating Credentials
var passWord = new SecureString();
foreach (var c in PWD) passWord.AppendChar(c);
var credential = new SharePointOnlineCredentials(USER, passWord);
//Creating Handler to allows the client to use credentials and cookie
using (var handler = new HttpClientHandler() { Credentials = credential })
{
//Getting authentication cookies
Uri uri = new Uri(webUrl);
handler.CookieContainer.SetCookies(uri, credential.GetAuthenticationCookie(uri));
//Invoking REST API
using (var client = new HttpClient(handler))
{
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
HttpResponseMessage response = await client.GetAsync(string.Format(RESTURL, webUrl)).ConfigureAwait(false);
response.EnsureSuccessStatusCode();
string jsonData = await response.Content.ReadAsStringAsync();
return jsonData;
}
}
}
jsonData object returns site properties. jsonData对象返回站点属性。
How I can do to read a file, for example test.txt, saved in Documents folder in "mysite"? 如何读取保存在“ mysite”中“文档”文件夹中的文件(例如test.txt)?
Do you mean that you want to get the content of file in SharePoint? 您是否要在SharePoint中获取文件的内容?
We can use CSOM to achieve it. 我们可以使用CSOM实现它。
Here is a sample for your reference: 这是一个示例供您参考:
public static void getContentOfFileInDocLib(string siteUrl, string host, string sourceListName, string fileName)
{
siteUrl = siteUrl.EndsWith("/") ? siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;
ClientContext context = new ClientContext(siteUrl);
Web web = context.Site.RootWeb;
List source = web.Lists.GetByTitle(sourceListName);
context.Load(source);
context.Load(web);
context.ExecuteQuery();
FileCollection files = source.RootFolder.Files;
Microsoft.SharePoint.Client.File file = files.GetByUrl(siteUrl + "/" + sourceListName + "/" + fileName);
context.Load(file);
context.ExecuteQuery();
FileInformation fileInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(context, file.ServerRelativeUrl);
string filePath = host + file.ServerRelativeUrl;
System.IO.Stream fileStream = fileInfo.Stream;
FileCreationInformation createFile = new FileCreationInformation();
byte[] bufferByte = new byte[1024 * 100];
System.IO.MemoryStream memory = new System.IO.MemoryStream();
int len = 0;
while ((len = fileStream.Read(bufferByte, 0, bufferByte.Length)) > 0)
{
memory.Write(bufferByte, 0, len);
}
//we get the content of file here
byte[] bytes = memory.GetBuffer();
//do something you want
//.......
}
Upload file to SharePoint Document Library. 将文件上传到SharePoint文档库。
public static void uploadFile(string siteUrl, string filePath, string fileName, string docLibName)
{
siteUrl = siteUrl.EndsWith("/") ? siteUrl.Substring(0, siteUrl.Length - 1) : siteUrl;
ClientContext context = new ClientContext(siteUrl);
List docLib = context.Web.Lists.GetByTitle(docLibName);
context.Load(docLib);
context.ExecuteQuery();
Byte[] bytes = System.IO.File.ReadAllBytes(filePath + fileName);
FileCreationInformation createFile = new FileCreationInformation();
createFile.Content = bytes;
createFile.Url = siteUrl + "/" + docLibName + "/" + fileName;
createFile.Overwrite = true;
Microsoft.SharePoint.Client.File newFile = docLib.RootFolder.Files.Add(createFile);
newFile.ListItemAllFields.Update();
context.ExecuteQuery();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.