Client Object Model是SharePoint 2010对开发提供新的支持。我前两周在给一个客户做POC,主要内容是将SharePoint 2010 作为一个纯内容管理平台,并隐藏起来不给最终用户看到,最终用户通过原有平台进行文档的增删查改操作。这些都是用默认的认证方式,认证及权限代码没有包含在内。以下是我截取其中的一些代码。
1 Client Object Model是SharePoint 2010对开发提供新的支持。我前两周在给一个客户做POC,主要内容是将SharePoint 2010 作为一个纯内容管理平台,并隐藏起来不给最终用户看到,最终用户通过原有平台进行文档的增删查改操作。这些都是用默认的认证方式,认证及权限代码没有包含在内。以下是我截取其中的一些代码。
2
3 在SharePoint 2010 中创建文档库
4
5 public string CreateDocumentLibrary(string siteUrl, string DocumentLibraryName,string userName)
6 {
7 ListCreationInformation newListInfo = new ListCreationInformation();
8 newListInfo.Title = DocumentLibraryName;
9 newListInfo.TemplateType = (int)ListTemplateType.DocumentLibrary;
10 List newList;
11 using (ClientContext clientContext = new ClientContext(siteUrl))
12 {
13 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
14 Web site = clientContext.Web;
15 newList = site.Lists.Add(newListInfo);
16 clientContext.Load(newList);
17 clientContext.uteQuery();
18 }
19 return "Create Success";
20 }
21
22
23 在SharePoint 2010 中删除文档库
24
25 public string DeleteDocumentLibrary(string siteUrl, string DocumentLibraryName)
26 {
27 using (ClientContext clientContext = new ClientContext(siteUrl))
28 {
29 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
30 Web site = clientContext.Web;
31 List existList = site.Lists.GetByTitle(DocumentLibraryName);
32 existList.DeleteObject();
33 clientContext.uteQuery();
34 clientContext.Dispose();
35 }
36 return "Delete Success";
37 }
38
39
40 在SharePoint 2010 中上传文档
41
42 public string UploadFileToDocumntLibrary(string siteUrl, string documentListName, string documentListURL, string documentName, byte[] documentStream)
43 {
44 using (ClientContext clientContext = new ClientContext(siteUrl))
45 {
46 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
47 List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
48 var fileCreationInformation = new FileCreationInformation();
49 fileCreationInformation.Content = documentStream;
50 fileCreationInformation.Overwrite = true;
51 fileCreationInformation.Url = siteUrl+ documentListURL+"/" + documentName;
52 Microsoft.SharePoint.Client.File uploadFile = documentsList.RootFolder.Files.Add(fileCreationInformation);
53 uploadFile.ListItemAllFields.Update();
54 clientContext.uteQuery();
55 }
56 return "upload success";
57 }
58
59
60 在SharePoint 2010 中下载文档
61
62 public byte[] DownloadDocument(string siteURL, string documentListName, string documentName)
63 {
64 ListItem item = GetDocumentFromSP(siteURL, documentListName, documentName);
65 if (item != null)
66 {
67 using (ClientContext clientContext = new ClientContext(siteURL))
68 {
69 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
70 FileInformation fInfo = Microsoft.SharePoint.Client.File.OpenBinaryDirect(clientContext, item["FileRef"].ToString());
71 Stream s = fInfo.Stream;
72 byte[] bt = ReadFully(s, 0);
73 return bt;
74 }
75 }
76 return null;
77 }
78
79
80 在SharePoint 2010 中获取文档库中的文档
81
82 public List<SharePointListItem> GetListItemCollection(string siteURL, string documentListName)
83 {
84 ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName,20);
85 List<SharePointListItem> lireturn = new List<SharePointListItem>();
86 foreach (ListItem li in listItems)
87 {
88 SharePointListItem item = new SharePointListItem();
89 item.Type = li.FileSystemObjectType.ToString();
90 item.DisplayName = li.FieldValues["FileLeafRef"].ToString();
91 item.FilePath = li.FieldValues["FileRef"].ToString();
92 item.CreatedDate =(DateTime)li.FieldValues["Created"];
93 item.ModifiedDate =(DateTime)li.FieldValues["Modified"];
94 item.Author = ((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Author"]).LookupValue;
95 item.Editor=((Microsoft.SharePoint.Client.FieldUserValue)li.FieldValues["Editor"]).LookupValue;
96 lireturn.Add(item);
97 }
98 return lireturn;
99 }
100
101
102 上面方法调用的方法
103
104 // Code by 丁为平
105 private ListItem GetDocumentFromSP(string siteURL, string documentListName, string documentName)
106 {
107 ListItemCollection listItems = GetListItemCollectionFromSP(siteURL, documentListName, "FileLeafRef",
108 documentName, "Text", 1);
109 return (listItems != null && listItems.Count == 1) ? listItems[0] : null;
110 }
111 private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, int rowLimit)
112 {
113 ListItemCollection listItems = null;
114 using (ClientContext clientContext = new ClientContext(siteURL))
115 {
116 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
117 List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
118 CamlQuery camlQuery = new CamlQuery();
119 camlQuery.ViewXml =
120 @"<View>
121 <Query>
122 <RowLimit>" + rowLimit.ToString() + @"</RowLimit>
123 </Query>
124 </View>";
125 listItems = documentsList.GetItems(camlQuery);
126 clientContext.Load(documentsList);
127 clientContext.Load(listItems);
128 clientContext.uteQuery();
129 }
130 return listItems;
131 }
132 private ListItemCollection GetListItemCollectionFromSP(string siteURL, string documentListName, string name, string value, string type, int rowLimit)
133 {
134 ListItemCollection listItems = null;
135 using (ClientContext clientContext = new ClientContext(siteURL))
136 {
137 clientContext.Credentials = System.Net.CredentialCache.DefaultCredentials;
138 List documentsList = clientContext.Web.Lists.GetByTitle(documentListName);
139 CamlQuery camlQuery = new CamlQuery();
140 camlQuery.ViewXml =
141 @"<View>
142 <Query>
143 <Where>
144 <Eq>
145 <FieldRef Name=""" + name + @"""/>
146 <Value Type=""" + type + """>" + value + @"</Value>
147 </Eq> </Where>
148 <RowLimit>" + rowLimit.ToString() + @"</RowLimit>
149 </Query>
150 </View>";
151 listItems = documentsList.GetItems(camlQuery);
152 clientContext.Load(documentsList);
153 clientContext.Load(listItems);
154 clientContext.uteQuery();
155 }
156 return listItems;
157 }
158 private byte[] ReadFully(Stream stream, int initialLength)
159 {
160 // If we""ve been passed an unhelpful initial length, just
161 // use 32K.
162 if (initialLength < 1)
163 {
164 initialLength = 32768;
165 }
166 byte[] buffer = new byte[initialLength];
167 int read = 0;
168 int chunk;
169 while ((chunk = stream.Read(buffer, read, buffer.Length - read)) > 0)
170 {
171 read += chunk;
172 // If we""ve reached the end of our buffer, check to see if there""s
173 // any more information
174 if (read == buffer.Length)
175 {
176 int nextByte = stream.ReadByte();
177 // End of stream? If so, we""re done
178 if (nextByte == -1)
179 {
180 return buffer;
181 }
182 byte[] newBuffer = new byte[buffer.Length * 2];
183 Array.Copy(buffer, newBuffer, buffer.Length);
184 newBuffer[read] = (byte)nextByte;
185 buffer = newBuffer;
186 read++;
187 }
188 }
189 byte[] ret = new byte[read];
190 Array.Copy(buffer, ret, read);
191 return ret;
192 }
193
194
195 还有一个支持类
196
197 [Serializable]
198 public class SharePointListItem
199 {
200 public SharePointListItem()
201 {
202 }
203 public string DisplayName { get; set; }
204 public string FilePath { get; set; }
205 public DateTime CreatedDate { get; set; }
206 public DateTime ModifiedDate { get; set; }
207 public string Author { get; set; }
208 public string Editor { get; set; }
209 public string Type { get; set; }
210 }
浙公网安备 33010602011771号