65.9K
CodeProject 正在变化。 阅读更多。
Home

WSS .NET 集成

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (3投票s)

2010 年 6 月 15 日

CPOL

8分钟阅读

viewsIcon

34619

downloadIcon

451

.NET 编程 WSS 或 Sharepoint

引言

Microsoft Windows SharePoint Services 3.0 提供协作工具,帮助人们跨越组织和地理界限保持联系。Windows SharePoint Services 使人们能够访问所需信息。Windows SharePoint Services 基于 Microsoft Windows Server 2003 构建,还为构建可轻松灵活地满足业务不断变化和增长需求的基于 Web 的业务应用程序提供了基础平台。

有时项目团队需要以编程方式与 SharePoint 服务进行通信。有多种方法可以以编程方式与 SharePoint 进行通信。

  • 通过 SharePoint 站点公开的 SharePoint Web 服务
  • 通过 SharePoint 对象模型
  • 通过自定义 Web 服务公开 SharePoint 对象模型

本文将尝试涵盖这些方法及其优缺点。

Windows SharePoint Services Web 服务

Windows SharePoint Services Web 服务提供可用于远程处理 Microsoft Windows SharePoint Services 部署的方法。Sharepoint 服务公开了一组 Web 服务,客户端应用程序可以消费这些 Web 服务来访问站点和文档库。下图代表了高层通信图。

Figure1.jpg

下表提供了 WSS 公开的服务及其用法的高层描述。

Service

描述

Administration

提供管理 Windows SharePoint Services 部署的方法,例如创建或删除站点。

警报

提供处理 SharePoint 站点中列表项的警报的方法。

文档工作区

公开 Document Workspace Web 服务及其十一个方法,用于管理 Document Workspace 站点及其包含的数据。

表单

提供在处理列表内容时用于用户界面的表单的方法。

成像

提供使您能够创建和管理图片库的方法。

列表数据检索

提供了一种对 Microsoft Windows SharePoint Services 中的列表执行查询的方法。

列表

提供处理列表和列表数据的方法。

会议

提供使您能够创建和管理 Meeting Workspace 站点的方法。

权限

提供处理站点或列表权限的方法。

站点数据

提供从 Microsoft Windows SharePoint Services 中的站点或列表返回元数据或列表数据的方法。

站点

提供了一种返回有关站点集合的站点模板信息的方法。

用户和组

提供处理用户、站点组和跨站点组的方法。

版本

提供处理文件版本的方法。

视图

提供处理列表视图的方法。

Web 部件页面

提供将信息发送到 XML Web 服务并从中检索信息的方法。

Webs

有关公开的服务和方法的详细信息,请参阅以下链接:

操作指南

以下部分介绍了如何使用 .NET 客户端获取 SharePoint 站点下可用的文档库列表。使用 Java 等其他编程语言与 WSS Web 服务通信的概念是类似的。客户端必须使用 Web 服务公开的 WSDL 创建代理对象,然后调用 Web 服务以使用 Web 服务公开的 Web 方法。

我们假设我们有一个名为 MySite 的站点,可以通过以下 URL 访问:

所有 Web 服务都将位于路径:

步骤 1:将 Web 引用添加到 WSS Web 服务

List Web 服务(http://servername:34635/sites/MySite/_vti_bin/Lists.asmx)公开了查询站点中可用文档库所需的 Web 方法。

通过在项目资源管理器中单击“添加 Web 引用”来将 Web 引用添加到 Web 站点。

Figure2.jpg

步骤 2:在代码中调用 Web 服务

List 服务的 GetListCollection() 方法用于获取站点中可用文档库的列表。为了调用此方法,开发人员首先必须创建代理对象实例,然后该实例将调用 WSS 公开的 Web 方法。

 // proxy object to call the Lists web service
List.Lists ListsWebService = new List.Lists();

// the user credentials to use
ListsWebService.Credentials = new NetworkCredential("username", "password", "domain"); ;
ListsWebService.Url = "http://servername:34635/sites/MySite/_vti_bin/Lists.asmx";

try
{
    // get list of all lists
    var getListXMLNode = ListsWebService.GetListCollection();
    
    // Parse the XML to XElement
    XElement getListXElement = XElement.Parse(getListXMLNode.OuterXml.ToString());
    
    //Using .NET 3.5 feature to select only the required set of fields and filter 
    //collection of type document library.
    var ListCollection = getListXElement.Elements().Select(e => new
    ListCollection
    {
        DocTemplateUrl = e.Attribute("DocTemplateUrl").Value.ToString(),
        DefaultViewUrl = e.Attribute("DefaultViewUrl").Value.ToString(),
        Title = e.Attribute("Title").Value.ToString(),
        Description = e.Attribute("Description").Value.ToString(),
        BaseType = Convert.ToInt32(e.Attribute("BaseType").Value.ToString()),
        ServerTemplate = Convert.ToInt32(e.Attribute("ServerTemplate").Value.ToString())
    });
    
    if (serverTemplate > 0)
    {
        ListCollection = ListCollection.Where(l => l.ServerTemplate == serverTemplate);
    }
    return ListCollection.ToList();
}
获取的数据将采用 XML 格式,包含站点中的所有列表。这需要进一步处理以获取站点中可用的文档库列表。完整的代码将在本文末尾提供。

SharePoint 对象模型

Windows SharePoint Services 提供了一个高度结构化的服务器端对象模型,可以轻松访问代表 SharePoint Web 站点各个方面的对象。从更高级别的对象,您可以深入对象层次结构以获取包含您需要在代码中使用的成员的对象。如果代码将在与 WSS 相同的计算机上执行,则可以使用 SharePoint 对象模型。要处理 SharePoint 对象模型,开发人员必须引用位于以下目录中的 Microsoft.SharePoint.dll

C:\program files\common files\microsoft shared\web server extensions\12\isapi 

Figure3.jpg

有关服务器体系结构和对象模型层次结构的信息,请参阅以下链接:

操作指南

以下部分介绍了如何使用 SharePoint 对象模型获取 SharePoint 站点下可用的文档库列表。

步骤 1:将引用添加到 Microsoft.SharePoint.dll

在安装了 WSS 的计算机上,从以下位置添加对 SharePoint 库的引用。

C:\program files\common files\microsoft shared\web server extensions\12\isapi 

注意:开发人员可以将 Microsoft.SharePoint.dll 复制到他们的系统并进行开发。但是,代码只能在安装了 WSS 或 SharePoint 的计算机上执行。

步骤 2:对象模型编码

Microsoft.Sharepoint.dll 公开了多个类和方法,可用于查询和操作 SharePoint 集合/站点和列表。以下代码获取类型为文档库的列表。

using (SPSite siteCollection = new SPSite(sitePath))
{
    List folderList = new List();
    SPWeb web = siteCollection.OpenWeb();
    
    web.AllowUnsafeUpdates = true;
    SPListCollection collection = web.GetListsOfType(SPBaseType.DocumentLibrary);
    FolderProperties folder;
    foreach (SPList list in collection)
    {
        folder = new FolderProperties();
        if (!list.Hidden)
        {
            folder.RelativeURL = list.Url();
            folder.Title = list.Title;
            folder.Name = list.Name();
            folderList.Add(folder);
        }
    }
    return folderList;
}

SharePoint 服务 vs. 对象模型

如果最终应用程序将在 SharePoint 服务器上执行,则可以使用 SharePoint 对象模型。因此,如果最终应用程序托管在与 SharePoint 服务器相同的计算机上,或者您正在编写一些 Web 部件,则可以使用此模型。

如果您正在开发一个将远程与 WSS 3.0 通信的应用程序,那么处理它们的最佳方法是使用 WSS 3.0 公开的 Web 服务。

注意:您可以将 Microsoft.Sharepoint.dll 复制到开发计算机并在应用程序中使用。但是,此 Microsoft.Sharepoint.dll 库不会在开发计算机上加载。您需要在安装了 SharePoint 服务器的计算机上部署它才能执行相同的操作。

带有对象模型代码的自定义 Web 服务。

有时应用程序需要部署在远程计算机上并使用对象模型访问 SharePoint 服务器。在这些情况下,建议通过 Web 服务公开所需的功能,该 Web 服务将部署在与 SharePoint 相同的计算机上。既然 Web 服务已部署在同一台计算机上,就可以使用 SharePoint 对象模型进行开发。远程应用程序可以访问此 Web 服务公开的功能。

Figure4.jpg

操作指南

以下部分介绍了如何使用托管在 SharePoint 计算机/服务器上的自定义服务来获取 SharePoint 站点下可用的文档库列表。

步骤 1:创建自定义 Web 服务

使用 Visual Studio 创建 Web 服务项目。

Figure5.jpg

在安装了 WSS 的计算机上,从以下位置添加对 SharePoint 库的项目引用。

C:\program files\common files\microsoft shared\web server extensions\12\isapi 

添加必要的 Web 方法以使用对象模型访问 SharePoint 服务器。以下 Web 方法将返回站点路径下所有可用的文档库。

[WebMethod]
public List<folderproperties> GetDocumentLibaries(string sitePath)
{
    using (SPSite siteCollection = new SPSite(subSitePath))
    {
        List<folderproperties> folderList = new List<folderproperties>();
        SPWeb web = siteCollection.OpenWeb();
        
        try
        {
            web.AllowUnsafeUpdates = true;
            SPListCollection collection = web.GetListsOfType(SPBaseType.DocumentLibrary);
            FolderProperties folder;
            foreach (SPList list in collection)
            {
                folder = new FolderProperties();
                if (!list.Hidden)
                {
                    folder.RelativeURL = list.Url();
                    folder.Title = list.Title;
                    folder.Name = list.Name();
                    folderList.Add(folder);
                }
            }
            return folderList;
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            web.Close();
        }
    }
}

步骤 2:添加对上述创建的自定义服务的引用

通过在项目资源管理器中单击“添加 Web 引用”来将 Web 引用添加到客户端应用程序。

步骤 3:在代码中调用 Web 服务

服务的 GetDocumentLibaries() 方法用于获取站点中可用的文档库列表。为了调用此方法,开发人员首先必须创建代理对象实例,然后该实例将调用 Web 服务公开的 Web 方法。

CustomSharePointService client = new CustomSharePointService();
client.Credentials = new NetworkCredential(UserName, Password, Domain);
GridView1.DataSource = client.GetDocumentLibaries
			("http://mysheceusd01:24832/sites/MySite/");
GridView1.DataBind();

问题与解决

以下各节介绍了一些在使用自定义服务对象模型时遇到的问题以及如何解决这些问题。

问题:尝试在代码中使用 {(SPSite siteCollection = new SPSite(subSitePath))} 访问 SharePoint 时出现文件未找到错误。

上述问题可能由多种原因引起。对象模型代码在多种场景下会引发类似的 FileNotFound 消息,例如:

  • 错误的 SharePoint URL
  • 访问 SharePoint 站点的权限不足
  • 应用程序池问题

解决方案:因此,如果您遇到上述问题,请按照以下步骤调试该问题。

  1. 在浏览器中检查 URL,并检查用户是否有足够的权限访问该站点。
  2. 检查来自 Windows 或控制台应用程序的代码。
  3. 如果步骤 2 成功,请检查 Web 服务或包含对象模型代码的网站的应用程序池。将其更改为您尝试连接的站点集合的应用程序池。
  4. 如果步骤 3 仍然无效,请检查 web.config 和 IIS 中的以下内容:
    1. web.config 中有 <identity impersonate ="true"/>
    2. 在 IIS 设置中启用了 Windows 身份验证。

摘要

以下矩阵提供了每种开发方法的比较,帮助开发人员选择正确的开发和部署方法。

参数

开发方法

WSS 服务

对象模型

带有对象模型的自定义 Web 服务

开发便捷性

媒体

媒体

远程访问

灵活性

媒体

参考文献

代码

本节包含运行演示应用程序所需的代码。它有两个项目。

  1. CustomSharePointServices 包含自定义服务项目,其中包含通过 Web 服务公开的对象模型代码。这里有所有可以即用即改的方法,但有些情况下可能不是最优代码。 :)
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using Microsoft.SharePoint;
    using System.Web.Services.Protocols;
    using System.Xml;
    
    namespace CustomSharePointServices
    {
        /// <summary>
        /// Summary description for CustomSharePointService
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [System.ComponentModel.ToolboxItem(false)]
        // To allow this Web Service to be called from script, using ASP.NET AJAX, 
        // uncomment the following line. 
        // [System.Web.Script.Services.ScriptService]
        public class CustomSharePointService : System.Web.Services.WebService
        {
            public static readonly XmlQualifiedName DetailElementName = 
    					new XmlQualifiedName("detail");
            public static readonly string FileNotFound = "File not found";
            public static readonly string VersionNotFound = "Version Not Found";
            public static readonly string FileNotCheckedOut = "File not checked out";
    
            [WebMethod]
            public bool CreateDocLib(string docLibPath, string name, string description)
            {
                using (SPSite siteCollection = new SPSite(docLibPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPListTemplateType templateType = 
    				SPListTemplateType.DocumentLibrary;
                        Guid listId = web.Lists.Add(name, description, templateType);
                        SPList documentLibrary = web.Lists[listId];
                        documentLibrary.EnableVersioning = true;
                        documentLibrary.Update();
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public bool DocumentCheckOut(string documentPath)
            {
                bool retvalue = false;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    web.AllowUnsafeUpdates = true;
    
                    SPFile listFile = web.GetFile(documentPath);
                    if (listFile != null && listFile.Exists)
                    {
                        if (listFile.CheckOutStatus == SPFile.SPCheckOutStatus.None)
                        {
                            listFile.CheckOut();
                            retvalue = true;
                        }
                    }
                    else
                    {
                        throw new SoapException(FileNotFound, DetailElementName);
                    }
                    web.Close();
                }
                return retvalue;
            }
    
            [WebMethod]
            public bool DocumentUnDoCheckOut(string documentPath)
            {
                bool retvalue = false;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    web.AllowUnsafeUpdates = true;
    
                    SPFile listFile = web.GetFile(documentPath);
                    if (listFile != null && listFile.Exists)
                    {
                        if (listFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                        {
                            listFile.UndoCheckOut();
                            retvalue = true;
                        }
                    }
                    else
                    {
                        throw new SoapException(FileNotFound, DetailElementName);
                    }
                    web.Close();
                }
                return retvalue;
            }
    
            [WebMethod]
            public bool DocumentCheckIN(string documentPath, string comment)
            {
                bool retvalue = false;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            if (listFile.CheckOutStatus != SPFile.SPCheckOutStatus.None)
                            {
                                listFile.CheckIn(comment);
                                retvalue = true;
                            }
                        }
                        else
                        {
                            throw new SoapException
    			(FileNotCheckedOut, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return retvalue;
            }
    
            [WebMethod]
            public bool UploadDocument(string documentPath, byte[] contents)
            {
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            listFile.SaveBinary(contents);
    
                        }
                        else
                        {
                            listFile = web.Files.Add(documentPath, contents);
                        }
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public List<string> GetDocumentVersions(string documentPath)
            {
                List<string> versionList = null;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            SPFileVersionCollection versions = listFile.Versions;
                            versionList = new List<string>();
                            foreach (SPFileVersion item in versions)
                            {
                                versionList.Add(item.VersionLabel);
                            }
                        }
                        else
                        {
                            throw new SoapException(FileNotFound, DetailElementName);
                        }
                        web.Close();
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return versionList;
            }
    
            [WebMethod]
            public bool ReNameDocument(string documentPath, string newName)
            {
                bool retvalue = false;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            listFile.Item["Name"] = string.Format("{0}", newName);
                            listFile.Item.Update();
                            retvalue = true;
                        }
                        else
                        {
                            throw new SoapException(FileNotFound, DetailElementName);
                        }
                        web.Close();
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return retvalue;
            }
    
            [WebMethod]
            public byte[] DownloadDocumentByVersion
    			(string documentPath, string versionID)
            {
                byte[] content = null;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            if (listFile.UIVersionLabel == versionID)
                            {
                                content = listFile.OpenBinary();
                                return content;
                            }
    
                            SPFileVersionCollection versions = listFile.Versions;
                            foreach (SPFileVersion item in versions)
                            {
                                if (item.VersionLabel == versionID)
                                {
                                    content = item.OpenBinary();
                                    return content;
                                }
                            }
    
                            throw new SoapException(VersionNotFound, DetailElementName);
                        }
                        else
                        {
                            throw new SoapException(VersionNotFound, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public byte[] DownloadDocument(string documentPath)
            {
                byte[] content = null;
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            content = listFile.OpenBinary();
                            return content;
                        }
                        else
                        {
                            throw new SoapException(FileNotFound, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public bool DeleteDocument(string documentPath)
            {
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            listFile.Delete();
                        }
                        else
                        {
                            throw new SoapException(FileNotFound, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public bool DeleteDocumentByVersion(string documentPath, string versionID)
            {
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            SPFileVersionCollection versions = listFile.Versions;
                            foreach (SPFileVersion item in versions)
                            {
                                if (item.VersionLabel == versionID)
                                {
                                    item.Delete();
                                    break;
                                }
                            }
                        }
                        else
                        {
                            throw new SoapException(VersionNotFound, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public bool DeleteDocumentAllVersions(string documentPath)
            {
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            SPFileVersionCollection versions = listFile.Versions;
                            versions.DeleteAll();
                        }
                        else
                        {
                            throw new SoapException(VersionNotFound, DetailElementName);
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public bool CreateFolder(string docLibPath, string name, string description)
            {
                using (SPSite siteCollection = new SPSite(docLibPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFolderCollection folders = 
    			web.GetFolder(docLibPath).SubFolders;
    
                        folders.Add(name);
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return true;
            }
    
            [WebMethod]
            public List<string> GetFolders(string docLibPath)
            {
    
    
                using (SPSite siteCollection = new SPSite(docLibPath))
                {
                    List<string> folderList = new List<string>();
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFolderCollection folders = 
    			web.GetFolder(docLibPath).SubFolders;
    
    
                        foreach (SPFolder folder in folders)
                        {
                            if (folder.Name.ToLower() != "forms")
                            {
                                folderList.Add(folder.Name);
                            }
                        }
                        return folderList;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
    
            [WebMethod]
            public List<folderproperties> GetDocumentLibaries(string subSitePath)
            {
                using (SPSite siteCollection = new SPSite(subSitePath))
                {
                    List<folderproperties> folderList = new List<folderproperties>();
                    SPWeb web = siteCollection.OpenWeb();
    
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPListCollection collection = 
    			web.GetListsOfType(SPBaseType.DocumentLibrary);
                        FolderProperties folder;
                        foreach (SPList list in collection)
                        {
                            folder = new FolderProperties();
                            if (!list.Hidden)
                            {
                                folder.RelativeURL = list.Url();
                                folder.Title = list.Title;
                                folder.Name = list.Name();
                                folderList.Add(folder);
                            }
                        }
                        return folderList;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public List<fileproperties> GetDocumentLibariesFiles
    		(string docLibPath, string documentLibraryName)
            {
                using (SPSite siteCollection = new SPSite(docLibPath))
                {
                    List<fileproperties> fileList = new List<fileproperties>();
                    //List<string> fileList = new List<string>();
                    FileProperties fileProperties;
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        SPFileCollection f = web.GetFolder(documentLibraryName).Files;
                        foreach (SPFile file in f)
                        {
                            fileProperties = new FileProperties();
                            fileProperties.RelativeURL = file.Url;
                            fileProperties.AbsoulteURL = docLibPath + file.Url;
                            fileProperties.LastModified = 
    				file.TimeLastModified.ToString();
                            fileProperties.FileName = file.Name;
                            fileProperties.Title = file.Title;
                            fileProperties.isCheckedOut = 
    				file.CheckOutStatus.ToString();
                            if (file.CheckedOutBy != null)
                            {
                                fileProperties.CheckedOutBy = 
    				file.CheckedOutBy.Name.ToString();
                            }
                            fileProperties.FileCreatedDate = file.Author.Name;
                            fileProperties.FileCreatedBy = file.TimeCreated.ToString();
                            fileProperties.FileModifiedDate = 
    				file.TimeLastModified.ToString();
                            fileProperties.FileModifiedBy = file.ModifiedBy.Name;
                            fileList.Add(fileProperties);
                        }
                        return fileList;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public List<string> GetFileFromFolder(string docLibPath, string folderName)
            {
                using (SPSite siteCollection = new SPSite(docLibPath))
                {
                    List<string> fileList = new List<string>();
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFolder folder = 
    			web.GetFolder(docLibPath).SubFolders[folderName];
                        foreach (SPFile file in folder.Files)
                        {
                            fileList.Add(file.Name);
                        }
                        return fileList;
                    }
                    catch (Exception)
                    {
                        throw;
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public bool IsFolderExists(string FolderPath)
            {
                bool retvalue = false;
                using (SPSite siteCollection = new SPSite(FolderPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFolder folder = web.GetFolder(FolderPath);
                        if (folder != null && folder.Exists)
                        {
                            retvalue = true;
                        }
                        else
                        {
                            retvalue = false;
                        }
                        web.Close();
                    }
                    finally
                    {
                        web.Close();
                    }
                }
                return retvalue;
            }
    
    
            [WebMethod]
            public bool IsDocumentExists(string documentPath)
            {
                using (SPSite siteCollection = new SPSite(documentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
    
                        SPFile listFile = web.GetFile(documentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                }
            }
    
            [WebMethod]
            public bool CopyFile(string srcDocumentPath, string destinationDocumentPath)
            {
                bool retValue;
                using (SPSite siteCollection = new SPSite(srcDocumentPath))
                {
                    SPWeb web = siteCollection.OpenWeb();
                    try
                    {
                        web.AllowUnsafeUpdates = true;
                        SPFile listFile = web.GetFile(srcDocumentPath);
                        if (listFile != null && listFile.Exists)
                        {
                            listFile.CopyTo(destinationDocumentPath);
                            retValue = true;
                        }
                        else
                        {
                            retValue = false;
                        }
                    }
                    finally
                    {
                        web.Close();
                    }
                    return retValue;
                }
            }
        }
    }
  2. Sample 是一个 Web 应用程序,其中包含使用 WSS Web 服务、对象模型以及通过消耗自定义 Web 服务来显示文档库的代码。

历史

  • 2010 年 6 月 15 日:初次发布
© . All rights reserved.