SharePoint .NET 集成






4.50/5 (2投票s)
将 SharePoint web 服务与 .NET 项目集成,以通过编程方式执行列表创建、文档获取和上传任务。
引言
我提供了示例代码,用于将 SharePoint 集成到您的项目中,以通过 C# 编程方式提供文档上传、文档列表和在 SharePoint 上创建文件夹的功能。
背景
SharePoint 服务器为我们提供了安全存储文档的设施。有时,我们必须在项目中集成 SharePoint 服务,以便用户可以在不通过 SharePoint 服务器的情况下访问文档。
要求
您应该在给定的 SharePoint 服务器上拥有一个用户帐户,并且您必须拥有足够的权限才能创建、上传和编辑文档或文件夹才能运行该代码。
在 SharePoint 上创建文件夹
目的
在文件夹或列表下在 SharePoint 中创建文件夹。
服务引用
在您的项目解决方案中添加一个 lists.asmx web 服务引用,使用给定的 URL,并将其命名为 ListServiceReference。
https://yoursharepointdomain/_vti_bin/lists.asmx
C# 代码
复制以下代码并粘贴到您的解决方案中。解决 .Net 框架中已经存在的一些 XML 相关引用。在下面的代码中,我在父文件夹或列表“共享文档”下创建了一个子文件夹,您可以使用您的任何一个文件夹或列表来代替,但它应该采用 URL 格式,例如
batchNode.SetAttribute("RootFolder", "https://yoursharepointdomain/<FolderName>");
或
batchNode.SetAttribute("RootFolder", "https://yoursharepointdomain/<ParentFolder>/<ChildFolder>");
示例
batchNode.SetAttribute("RootFolder", https://yoursharepointdomain/Shared Documents);
public void CreateFolderOnSharepoint(string listName, string newFolderName)
{
try
{
ListServiceReference.Lists listService = new ListServiceReference.Lists();
listService.Url = "https://yoursharepointdomain/_vti_bin/lists.asmx";
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
//Correct invalid characters
newFolderName = newFolderName.Replace(":", "_");
string xmlCommand = string.Format("<Method ID='1' Cmd='New'><Field Name='ID'>New</Field><Field Name='FSObjType'>1</Field><Field Name='BaseName'>{1}</Field></Method>", newFolderName);
XmlDocument doc = new XmlDocument();
System.Xml.XmlElement batchNode = doc.CreateElement("Batch");
batchNode.SetAttribute("OnError", "Continue");
batchNode.SetAttribute("RootFolder", https://yoursharepointdomain/ + listName);
batchNode.InnerXml = xmlCommand;
XmlNode resultNode = listService.UpdateListItems(listName, batchNode);
if ((resultNode != null) && (resultNode.FirstChild.FirstChild.InnerText == "0x8107090d") || (resultNode.FirstChild.FirstChild.InnerText == "0x00000000"))
{
// Folder successfully created
}
else
{
//error
//resultNode.OuterXml
}
}
catch (Exception)
{throw; } }
方法调用:像这样调用方法
CreateFolderOnSharepoint("Shared Documents","MyTestFolder");
成功运行代码后刷新页面,您现在将能够看到新文件夹。
在 SharePoint 上上传文件
目的
在 SharePoint 的文件夹或列表下上传文件。
服务引用
在您的项目解决方案中添加一个 copy.asmx web 服务引用,使用给定的 URL,并将其命名为 CopyServiceReference。
https://yoursharepointdomain/_vti_bin/copy.asmx
C# 代码
复制以下代码并粘贴到您的解决方案中。解决 .Net 框架中已经存在的一些 XML 相关引用。您可以使用您的任何一个目标文件夹或列表,但它应该采用 URL 格式,例如
string destinationFolderPath = "https://yoursharepointdomain/<FolderName>;
或
string destinationFolderPath = "https://yoursharepointdomain/<ParentName>/<ChildFolder>;
public void UploadFileOnSharePoint(string sourceFilePath, string FolderName)
{
string destinationFolderPath = “https://yoursharepointdomain/”+FolderName;
string[] strSplit = sourceFilePath.Split('\\');
string destinationPath = destinationFolderPath + "/" + strSplit[strSplit.Length - 1];
CopyServiceReference.Copy copyReference = new CopyServiceReference.Copy();
try
{
string destination = destinationPath;
string[] destinationUrl = { destination };
byte[] fileBytes = GetFileFromFileSystem(sourceFilePath);
copyReference.Url = "https://yoursharepointdomain/_vti_bin/copy.asmx";
copyReference.Credentials = System.Net.CredentialCache.DefaultCredentials;
CopyResult[] cResultArray = null;
FieldInformation documentTypeField = new FieldInformation();
documentTypeField.DisplayName = "DocumentType";
documentTypeField.Type = FieldType.Text;
documentTypeField.Value = " ";
FieldInformation titleField = new FieldInformation();
titleField.DisplayName = "Title";
titleField.Type = FieldType.Text;
titleField.Value = strSplit[strSplit.Length - 1];
//Linked Metadata For that file
FieldInformation[] filedInfo = { documentTypeField, titleField };
//Copy or upload file from your system to SharePoint server folder
copyReference.CopyIntoItems(destination, destinationUrl, filedInfo, fileBytes, out cResultArray);
}
catch (Exception ex)
{
//error
}
finally
{
if (copyReference != null)
copyReference.Dispose();
}
}
方法调用:像这样调用方法
UploadFileOnSharePoint(”C\\MyTestDocument.doc”,"Shared Documents");
成功运行代码后刷新页面,您现在将能够看到新文件。
从 SharePoint 获取文件
目的
获取 SharePoint 文件夹或列表下的所有项目的列表。(例如:“共享文档”)
服务引用
在您的项目解决方案中添加一个 lists.asmx web 服务引用,使用给定的 URL,并将其命名为 ListServiceReference。
https://yoursharepointdomain/_vti_bin/lists.asmx
C# 代码
复制以下代码并粘贴到您的解决方案中。解决 .Net 框架中已经存在的一些 XML 相关引用。 此外,在您的 ASPX 页面中添加一个带有 ID=lblFiles 的 Label。 在下面的代码中,我使用字符串创建了一个表格,以表格格式列出所有项目。
public void GetFilesFromSharePoint(string DirectoryName)
{
try
{
using (var client = new ListServiceReference.Lists())
{
client.Credentials = System.Net.CredentialCache.DefaultCredentials;
client.Url = "https://yoursharepointdomain/_vti_bin/lists.asmx";
XmlNode ndListItems = null;
XmlDocument xdoc = new XmlDocument();
XmlNode ndQuery = xdoc.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = xdoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = xdoc.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQuery.InnerXml = "";
ndViewFields.InnerXml = "";
ndQueryOptions.InnerXml = "<IncludeAttachmentUrls>TRUE</IncludeAttachmentUrls>";
ndListItems = client.GetListItems(DirectoryName, "", ndQuery, ndViewFields, "1000", ndQueryOptions, null);
if (ndListItems != null)
{
string strMain = "<table><tr><th>Title</th></tr>";
foreach (XmlNode node in ndListItems.ChildNodes)
{
XmlNodeReader objReader = new XmlNodeReader(node);
while (objReader.Read())
{
if (objReader["ows_EncodedAbsUrl"] != null && objReader["ows_LinkFilename"] != null)
{
string strURL = objReader["ows_EncodedAbsUrl"].ToString();
string strFileName = objReader["ows_LinkFilename"].ToString();
strMain = strMain + "<tr><td><a href=\"" + strURL + "\">" + strFileName + "</a></td></tr>";
}
}
}
lblFiles.Text = strMain + "</table>";
}
}
}
catch (Exception)
{ throw; } }
方法调用:使用文件夹名称调用方法,例如
GetFilesFromSharePoint(“Shared Documents”);
上述代码将在网页上附加一个表格,并列出该文件夹中的那些文件,作为 SharePoint 站点上该文档的链接。
如何添加 web 引用?
如果您对如何添加 web 服务 web 引用没有任何概念,那么您可以在 MSDN 上查看或按照以下步骤操作
- 右键单击您的项目解决方案名称。
- 转到 添加 > 服务引用。
- 现在点击左下方的“高级”按钮。
- 再次点击左下方的“添加 Web 引用”按钮。
- 在 Web 引用窗口下,将服务 URL 放入 URL 文本框中并按 Enter 键,现在您将能够查看与该服务相关的所有方法,就在该 URL 文本框下方,现在查看右侧文本框,位于“添加引用”按钮的上方。现在为该服务输入一个名称,或使用我在代码中提到的名称作为服务名称,然后单击“添加引用”按钮。
- 现在您将能够在您的项目中检查服务
关注点
我创建了一个非常简短的代码,以便您只需更改一些值就可以轻松地在您的项目中使用它。 就像即插即用一样。