动态构建 SharePoint Web 服务并访问文档






4.71/5 (13投票s)
本文介绍如何创建一个 Windows 应用程序解决方案,以访问 SharePoint 文档库中的文档。
引言
在本文中,我们将创建一个 Windows 应用程序解决方案来访问 SharePoint 文档库中的文档。通常,为了使用 Windows 应用程序访问资源,SharePoint 开发人员使用 SharePoint 对象模型。在这里,我们将使用 SharePoint 中提供的开箱即用的 Web 服务来访问文档。
SharePoint Web 服务
Web 服务是松散耦合的可重用软件组件,它们在语义上封装了离散的功能,并通过标准 Internet 协议进行分发并可通过编程方式访问。具体来说,Web 服务是一系列新兴标准,描述了面向服务的、基于组件的应用程序架构。从概念上讲,Web 服务代表了一种模型,其中电子商务流程中的离散任务在整个价值网络中广泛分布。XML Web 服务是向 Internet 上分布式计算发展的基本构建块。开放标准以及对人与应用程序之间通信和协作的关注,创造了一个 XML Web 服务正在成为应用程序集成平台的环境。
SharePoint 通过一组 Web 服务、WSS 3.0 (Windows SharePoint Services) 和 MOSS 2007 (Microsoft Office SharePoint Server) 支持互操作性和远程操作。Windows SharePoint Services Web 服务提供了可用于远程处理 Windows SharePoint Services 部署的方法。有多种以编程方式访问 SharePoint 的方法,例如对象模型、Web 服务、通过 HTTP 的远程过程调用以及基于 Web 的分布式创作和版本控制,所有这些方法都有其优点和适用之处,具体取决于解决方案的要求。SharePoint Web 服务构建在 SharePoint 对象模型之上,并公开了对象模型中可用功能的一个子集,并允许远程操作以及使用任何支持使用 Web 服务的编程语言和平台。
SharePoint Web 服务在 ASP.NET Web 服务 (ASMX) 中实现,您将在“Microsoft Shared”目录下的“web server extensions\12\ISAPI”中找到大多数这些 Web 服务的物理文件,通常位于“C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\ISAPI”。IIS 不包含任何子 Web 的应用程序或虚拟目录,但它们确实包含通过 SharePoint 元数据和 HttpModules 映射到 _vti_bin 虚拟目录。
用于 SharePoint 文档查看的 Windows 应用程序
在 Visual Studio 中创建一个 Windows 应用程序,并根据需要命名项目。现在,向您的 Windows 应用程序项目添加一个 .cs 文件,并将其命名为 WebServiceManaged.cs。在这里,我们将编写动态构建 Web 服务并在运行时获取方法访问权限的概念。现在设计 Windows 应用程序的用户界面。
用户界面是这样的,网站 URL 用于输入 SharePoint 网站 URL。然后,当单击“加载站点”按钮时,它将操作 URL 字符串并重新构建 SharePoint 网站的 Web 服务 URL,并将这些信息发送给 Web 服务调用。Web 服务返回下拉列表框中的文档库名称,当您从下拉框中选择特定的文档库时,文档库的相应文档将显示在数据视图下的数据网格中。
现在验证用户输入的 URL
// Regular expression to validate URL
Regex RgxUrl = new Regex(
"(([a-zA-Z][0-9a-zA-Z+\\-\\.]*:)?/{0,2}[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?" +
"(#[0-9a-zA-Z;/?:@&=+$\\.\\-_!~*'()%]+)?");
// Check the URL and assign to the string
if (RgxUrl.IsMatch(txt_site.Text))
strURLtxt = txt_site.Text.ToString();
// Manipulating the URL and get the site name
Uri uriO = new Uri(strURLtxt);
string sName =
uriO.AbsoluteUri.Replace(uriO.PathAndQuery, string.Empty);
在这里,让我们看看在我们的 C# 代码中动态构建 SharePoint Web 服务的过程。
借助反射和代理代码生成概念,我们正在动态构建 Web 服务。
// Generate the proxy code
CodeDomProvider provider1 = CodeDomProvider.CreateProvider("CSharp");
// Compile the assembly proxy with the appropriate references
string[] assemblyReferences = new string[5] { "System.dll", "System.Web.Services.dll",
"System.Web.dll", "System.Xml.dll", "System.Data.dll" };
CompilerParameters parms = new CompilerParameters(assemblyReferences);
CompilerResults results = provider1.CompileAssemblyFromDom(parms, unit1);
// Check For Errors
if (results.Errors.Count > 0)
{
foreach (CompilerError oops in results.Errors)
{
System.Diagnostics.Debug.WriteLine("========Compiler error============");
System.Diagnostics.Debug.WriteLine(oops.ErrorText);
}
throw new System.Exception(
"Compile Error Occured calling webservice. Check Debug ouput window.");
}
// Finally, Invoke the web service method
object wsvcClass = results.CompiledAssembly.CreateInstance(serviceName);
MethodInfo mi = wsvcClass.GetType().GetMethod(methodName);
((System.Web.Services.Protocols.WebClientProtocol)(wsvcClass)).Credentials =
netCredential;
在这里,我们调用函数来动态构建 Web 服务并获取要显示在 UI 中的 SharePoint 资源。
object args1 = WSprocess.CallWebService(
sName + "/_vti_bin/Lists.asmx", "Lists", "GetListCollection",
args);
Web 服务返回的 XML 数据将被操作并填充在下拉控件中。
System.Collections.Generic.List<KeyValuePair<string, string>>
ComBoData = new List<KeyValuePair<string,
string>>();
foreach (System.Xml.XmlElement xElm in xDoc.DocumentElement.ChildNodes)
{
if (xElm.Name.ToLower() == "list")
{
// check for the document library
if (xElm.GetAttribute("ServerTemplate").ToString() == "101")
ComBoData.Add(new KeyValuePair<string,
string>(xElm.GetAttribute("Title").ToString(),
xElm.GetAttribute("ID").ToString() + "~" +
xElm.GetAttribute("WebId").ToString()));
}
}
根据从下拉控件中选择的文档库名称,数据网格将相应地填充文档信息。
XmlNamespaceManager nsDocLib = new XmlNamespaceManager(xDocdetails.NameTable);
nsDocLib.AddNamespace("z", "#RowsetSchema");
XmlNodeList rows = xDocdetails.SelectNodes("//z:row", nsDocLib);
List<SharePointDataItemsInfo>
list = new List<SharePointDataItemsInfo>();
foreach (XmlNode row in rows)
{
string[] strfg = row.Attributes["ows_FileRef"].Value.ToString().Split('#');
if (row.Attributes["ows_DocIcon"].Value == "docx" ||
row.Attributes["ows_DocIcon"].Value == "doc")
{
SharePointDataItemsInfo cust1 = new SharePointDataItemsInfo(strfg[1].ToString(),
strfg[1].ToString(), strfg[0].ToString());
list.Add(cust1);
}
}
最重要的是,在运行应用程序之前,您首先要更改代码中的凭据。
System.Net.NetworkCredential
netCredential = new System.Net.NetworkCredential("URusername",
"URpassword", "URdomain");
SharePoint 中的列表 Web 服务用于检索列表信息 "/_vti_bin/Lists.asmx"。GetListItems
是用于从 SharePoint 列表中提取列表项的 Web 方法。这是我们构建的解决方案的输出。
希望本文对 SharePoint 开发人员有所帮助。感谢您的支持。
参考