SharePoint Web Service系列:编写自定义SharePoint Web Services之二
发布日期:2021-06-29 03:55:43 浏览次数:5 分类:技术文章

本文共 2694 字,大约阅读时间需要 8 分钟。

  下面,我们要将我们的Web服务添加到WSS的Web服务列表中,这样就可以在VS.NET中添加该Web服务的引用了。

1、打开spdisco.aspx文件,该文件位于Local_Drive:/Program Files/Common Files/Microsoft Shared/Web Server Extensions/60/ISAPI目录。

2、在文件末尾的discovery元素中添加下面的内容,保存。

<
contractRef 
ref
=<% 
SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx?wsdl", '"'); %
>
 docRef=
<
% SPEncode
.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx", '"'); %
>
 xmlns="http://schemas.xmlsoap.org/disco/scl/" />
<
soap 
address
=<% 
SPEncode.WriteHtmlEncodeWithQuote(Response, spWeb.Url + "/_vti_bin/Service1.asmx", '"'); %
>
 xmlns:q1="http://schemas.microsoft.com/sharepoint/soap/directory/" binding="q1:Service1Soap" xmlns="http://schemas.xmlsoap.org/disco/soap/" />

 

注意:soap元素的binding属性中在 "Soap"之前的文字(该例中的binding="q1:Service1Soap")指定了定义Web service时使用的类名。

 至此,我们的自定义Web service就部署完成了。我们可以像使用默认的Web service一样来调用我们的自定义Web service了。

创建文档上载Web服务

我们可以使用上面的方法来建立文档上载Web服务来上载文档到一个WSS的共享文档文档库。该服务利用服务虚拟化来获取站点上下文,然后上载文档到指定的文档库。

创建一个Web service项目,名为UploadSvc。添加一个新的Web service类命名为UploadFile。

添加对Windows SharePoint Services (Microsoft.Sharepoint.dll)的引用。

在UploadFile类中添加下面的Web方法:

 

[WebMethod]
public
 
string
 UploadDocument(
string
 fileName, 
byte
[] fileContents, 
string
 pathFolder)
{
    
if
 ( fileContents 
==
 
null
)
    {
        
return
 
"
Null Attachment
"
;
    }
   
try
   {
        SPWeb site 
=
 SPControl.GetContextWeb(Context);
        SPFolder folder 
=
 site.GetFolder(pathFolder);
        
string
 fileUrl 
=
 fileName;
        SPFile file 
=
 folder.Files.Add(fileUrl, fileContents);
        
return
 file.TimeCreated.ToLongDateString()
+
 
"
::
"
 
+
 file.Title;
   }
   
catch
 (System.Exception ee)
    {
        
return
 ee.Message 
+
 
"
::
"
 
+
 ee.Source;
    }
}

添加下面的命名空间引用:

using
 System.IO;
using
 Microsoft.SharePoint;
using
 Microsoft.SharePoint.WebControls;

编译该Web service项目。

创建并修改.disco和.wsdl并修改spdisco.aspx, 需要将上面的Service1 替换成UploadFile。分别保存为UploadFiledisco.aspx和UploadFilewsdl.aspx。

拷贝这些文件到_vti_bin, 拷贝对应得.dll到 _vti_bin/bin。

调用上载文件服务的例子

新建一个WinForm应用程序,添加Web引用,并将该引用命名为WSSServer。

添加一个button和两个textbox到默认的窗体,一个textbox用来输入上载文件的路径,另一个用来指定要上载到哪个文档库。如http://Server_Name/sites/Target_Site/Document_Library

添加下面的代码到button的Click事件中。

WSSServer.UploadFile svcDocLib 
=
 
new
 WSSServer.UploadFile();
svcDocLib.Credentials 
=
 CredentialCache.DefaultCredentials;
string
 strPath 
=
 textBox1.Text;
string
 strFile 
=
 strPath.Substring(strPath.LastIndexOf(
"
//
"
+
 
1
);
string
 strDestination 
=
 textBox2.Text;
 
FileStream fStream 
=
 
new
 FileStream(strPath, System.IO.FileMode.Open);
byte
[] binFile 
=
 
new
 
byte
[(
int
)fStream.Length];
fStream.Read(binFile, 
0
, (
int
)fStream.Length);
fStream.Close();
 
string
 str 
=
 svcDocLib.UploadDocument(strFile, binFile, strDestination);
MessageBox.Show(str);

添加命名空间引用:

using
 System.Net;
using
 System.IO;

编译运行。

(结束)

转载地址:https://blog.csdn.net/zengjibing/article/details/3816317 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:SharePoint Web Service系列: Add或Update其他各种类型的项
下一篇:SharePoint Web Service系列: Add或Update类型为User的项

发表评论

最新留言

留言是一种美德,欢迎回访!
[***.207.175.100]2024年04月21日 17时58分44秒

关于作者

    喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!

推荐文章

paip.软件及网站项目开发效率低下的思索与改进 2019-04-29
Atitit 可移植性之道attilax著 2019-04-29
paip.截屏功能流程说明 2019-04-29
Atiitt uke兼wag集团2017年度成果报告总结 attilax著 1. 组织机构进一步完善 8大首席部门 1 2. 事业部进一步完善,以及一百多个事业部了 1 3. 企业文化进一步完善 1 2019-04-29
Atititi ui之道 attilax著 v3 s11.docx 1. 概览 2 1.1. 软件设计可分为两个部分:编码设计与UI设计 2 2. 用户界面设计的三大原则是:置界面于用户的控制之下; 2019-04-29
Atitit 集团与个人的完整入口列表 attilax的完整入口 1. 集团与个人的完整入口列表 1 2. 流量入口概念 2 3. 流量入口的历史与发展 2 1.集团与个人的完整入口列表 2019-04-29
Atitit 网络编程之道 2019-04-29
Atiitt attilax掌握的前后技术放在简历里面.docx 2019-04-29
Atiitt 文档处理之道 attilax著 2019-04-29
Atiitt 可视化 报表 图表之道 attilax著 Atitit.可视化与报表原理与概论 1. 信息可视化 1 2. Gui可视化 2 2.1. atitit 知识的可视化.docx 2 2019-04-29
paip.c#图片裁剪 2019-04-29
paip.html 及css调试工具---debugbar 2019-04-29
paip.项目开发效率提升之思索 2019-04-29
paip.项目开发效率提升之思索 2019-04-29
Atitit spring5 集成 mybatis 注解班 2019-04-29
Atitit springboot mybatis spring 集成 Springboot1.4 mybatis3.4.6 /springbootMybatis 目录 1.1. 设置map 2019-04-29
Atitit 模板引擎总结 目录 1. 模板引擎 1 2. 常见模板步骤 1 2.1. 1)定义模板字符串 1 2.2. 2)预编译模板 2 2.3. 渲染模板 2 3. 流程渲染 if el 2019-04-29
Atitit 字符串转换数组main参数解析 args splitByWholeSeparator String string=" -host 101.1 8*124 -db 1 2019-04-29
paip.提升效率----几款任务栏软件vc59 2019-04-29
paip.验证码识别---序列号的反转 2019-04-29