使用SharePoint 2010 客户端对象模型进行文档库及文档的操作
- Client Object Model是SharePoint 2010对开发提供新的支持。我前两周在给一个客户做POC,主要内容是将SharePoint 2010 作为一个纯内容管理平台,并隐藏起来不给最终用户看到,最终用户通过原有平台进行文档的增删查改操作。这些都是用默认的认证方式,认证及权限代码没有包含在内。以下是我截取其中的一些代码。
private byte[] ReadFully(Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
在SharePoint 2010 中创建文档库
public string CreateDocumentLibrary(string siteUrl, string DocumentLibraryName,string userName)
{
ListCreationInformation newListInfo = new ListCreationInformation();
newListInfo.Title = DocumentLibraryName;
newListInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
List newList;
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web site = clientContext.Web;
newList = site.Lists.Add(newListInfo);
clientContext.Load(newList);
clientContext.ExecuteQuery();
}
{
ListCreationInformation newListInfo = new ListCreationInformation();
newListInfo.Title = DocumentLibraryName;
newListInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
List newList;
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web site = clientContext.Web;
newList = site.Lists.Add(newListInfo);
clientContext.Load(newList);
clientContext.ExecuteQuery();
}
return "Create Success";
}
}
在SharePoint 2010 中删除文档库
public string DeleteDocumentLibrary(string siteUrl, string DocumentLibraryName)
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web site = clientContext.Web;
List existList = site.Lists.GetByTitle(DocumentLibraryName);
existList.DeleteObject();
clientContext.ExecuteQuery();
clientContext.Dispose();
}
return "Delete Success";
}
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
Web site = clientContext.Web;
List existList = site.Lists.GetByTitle(DocumentLibraryName);
existList.DeleteObject();
clientContext.ExecuteQuery();
clientContext.Dispose();
}
return "Delete Success";
}
在SharePoint 2010 中上传文档
public string UploadFileToDocumntLibrary(string siteUrl, string documentListName, string documentListURL, string documentName, byte[] documentStream)
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = documentStream;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = siteUrl+ documentListURL+"/" + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
return "upload success";
}
{
using (ClientContext clientContext = new ClientContext(siteUrl))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
var fileCreationInformation = new FileCreationInformation();
fileCreationInformation.Content = documentStream;
fileCreationInformation.Overwrite = true;
fileCreationInformation.Url = siteUrl+ documentListURL+"/" + documentName;
Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
uploadFile.ListItemAllFields.Update();
clientContext.ExecuteQuery();
}
return "upload success";
}
在SharePoint 2010 中下载文档
public byte[] DownloadDocument(string siteURL, string documentListName, string documentName)
{
ListItem item = GetDocumentFromSP(siteURL, documentListName, documentName);
if (item != null)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());
Stream s = fInfo.Stream;
byte[] bt = ReadFully(s, 0);
return bt;
}
}
return null;
}
{
ListItem item = GetDocumentFromSP(siteURL, documentListName, documentName);
if (item != null)
{
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());
Stream s = fInfo.Stream;
byte[] bt = ReadFully(s, 0);
return bt;
}
}
return null;
}
在SharePoint 2010 中获取文档库中的文档
public List<SharePointListItem> GetListItemCollection(string siteURL, string documentListName)
{
ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName,20);
List<SharePointListItem> lireturn = new List<SharePointListItem>();
foreach (ListItem li in listItems)
{
SharePointListItem item = new SharePointListItem();
item.Type = li.FileSystemObjectType.ToString();
item.DisplayName = li.FieldValues["FileLeafRef"].ToString();
item.FilePath = li.FieldValues["FileRef"].ToString();
item.CreatedDate =(DateTime)li.FieldValues["Created"];
item.ModifiedDate =(DateTime)li.FieldValues["Modified"];
item.Author = ((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Author"]).LookupValue;
item.Editor=((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Editor"]).LookupValue;
lireturn.Add(item);
}
return lireturn;
}
{
ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName,20);
List<SharePointListItem> lireturn = new List<SharePointListItem>();
foreach (ListItem li in listItems)
{
SharePointListItem item = new SharePointListItem();
item.Type = li.FileSystemObjectType.ToString();
item.DisplayName = li.FieldValues["FileLeafRef"].ToString();
item.FilePath = li.FieldValues["FileRef"].ToString();
item.CreatedDate =(DateTime)li.FieldValues["Created"];
item.ModifiedDate =(DateTime)li.FieldValues["Modified"];
item.Author = ((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Author"]).LookupValue;
item.Editor=((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Editor"]).LookupValue;
lireturn.Add(item);
}
return lireturn;
}
上面方法调用的方法
// Code by 丁为平
private ListItem GetDocumentFromSP(string siteURL, string documentListName, string documentName)
{
ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName, "FileLeafRef",
documentName, "Text", 1);
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}
private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, int rowLimit)
{
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, string name, string value, string type, int rowLimit)
{
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='" + name + @"'/>
<Value Type='" + type + "'>" + value + @"</Value>
</Eq> </Where>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
{
ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName, "FileLeafRef",
documentName, "Text", 1);
return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
}
private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, int rowLimit)
{
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, string name, string value, string type, int rowLimit)
{
ListItemCollection listItems = null;
using (ClientContext clientContext = new ClientContext(siteURL))
{
clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
CamlQuery camlQuery = new CamlQuery();
camlQuery.ViewXml =
@"<View>
<Query>
<Where>
<Eq>
<FieldRef Name='" + name + @"'/>
<Value Type='" + type + "'>" + value + @"</Value>
</Eq> </Where>
<RowLimit>" + rowLimit.ToString() + @"</RowLimit>
</Query>
</View>";
listItems = documentsList.GetItems(camlQuery);
clientContext.Load(documentsList);
clientContext.Load(listItems);
clientContext.ExecuteQuery();
}
return listItems;
}
private byte[] ReadFully(Stream stream, int initialLength)
{
// If we've been passed an unhelpful initial length, just
// use 32K.
if (initialLength < 1)
{
initialLength = 32768;
}
byte[] buffer = new byte[initialLength];
int read = 0;
int chunk;
while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
{
read += chunk;
// If we've reached the end of our buffer, check to see if there's
// any more information
if (read == buffer.Length)
{
int nextByte = stream.ReadByte();
// End of stream? If so, we're done
if (nextByte == -1)
{
return buffer;
}
byte[] newBuffer = new byte[buffer.Length * 2];
Array.Copy(buffer, newBuffer, buffer.Length);
newBuffer[read] = (byte)nextByte;
buffer = newBuffer;
read++;
}
}
byte[] ret = new byte[read];
Array.Copy(buffer, ret, read);
return ret;
}
还有一个支持类
[Serializable]
public class SharePointListItem
{
public SharePointListItem()
{
}
public string DisplayName { get; set; }
public string FilePath { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
public string Author { get; set; }
public string Editor { get; set; }
public string Type { get; set; }
}
public class SharePointListItem
{
public SharePointListItem()
{
}
public string DisplayName { get; set; }
public string FilePath { get; set; }
public DateTime CreatedDate { get; set; }
public DateTime ModifiedDate { get; set; }
public string Author { get; set; }
public string Editor { get; set; }
public string Type { get; set; }
}
分类:
.Net技术
, SharePoint 2010
标签:
SharePoint 2010
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
· 手把手教你在本地部署DeepSeek R1,搭建web-ui ,建议收藏!
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 互联网不景气了那就玩玩嵌入式吧,用纯.NET开发并制作一个智能桌面机器人(三):用.NET IoT库
· 【非技术】说说2024年我都干了些啥