使用的工具是visual studio 2017。
新建项目可以是ASP.NET 和 Window Forms Application,使用C#。
在写代码之前需要添加reference,同时把namespace引用进来:
1)添加下面两个reference
- Microsoft.sharepoint.client.dll
- Microsoft.sharepoint.client.dll
如果没有的话可以从这里下载: https://www.microsoft.com/en-us/download/details.aspx?id=51679
2)添加引用
using Microsoft.sharepoint.client;
Notes: SharePoint server在另一台机子上面,所以写代码的时候不要每一行都有连接sharepoint server的请求, 最好把所有的请求整合一起,一次性连接server得到想要的结果。
以下是一个例子, SharePoint是online的。如果没有登录的过程的话,会报错403 error: The remote server returned an error: (403) Forbidden. 添加了登录的代码就好了。
using System;
using System.Security;
using System.Windows.Forms;
using Microsoft.SharePoint.Client;
namespace CSOM
{
public partial class Form1 : System.Windows.Forms.Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
using (ClientContext clientContext = new ClientContext(“https://XXX.sharepoint.com“))
{
SecureString passWord = new SecureString();
foreach (char c in "XXXXXX".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("[email protected]", passWord);
Web myweb = clientContext.Web;
ListCreationInformation listInformation = new ListCreationInformation();
listInformation.Title = "XXXX";
listInformation.Description = "XXXX";
listInformation.TemplateType = (int)ListTemplateType.Announcements;
List myList = myweb.Lists.Add(listInformation);
myList.OnQuickLaunch = true;
myList.Update();
clientContext.ExecuteQuery();
}
MessageBox.Show("List created");
}
}
}