Sharepoint2010应用开发五:使用WCF操作Sharepoint列表数据

使用WCF我们也可以对Sharepoint的列表数据进行操作。

主要有两种类型的WCF应用可以用来和Sharepoint的列表进行交互:

一、标准的WCF服务应用

二、REST-base ServiceREST(Representational State Transfer).

我们使用标准的WCF服务应用来和Sharepoint的列表进行交互。

首先,我们需要创建一个WCF,使用VS2010的项目模板,WCF Service Application,不要忘记选择的是.NET Framework3.5。我们需要创建一个服务契约和一个服务来和Sharepont的列表进行交互,其中,契约的代码如下:

 

ExpandedBlockStart.gif 代码
namespace  UpdateSPList
{
    [ServiceContract]
    
public   interface  IService1
    {

        [OperationContract]
        
void  updateBlog( string  SPSite,  string  blogTitle,  string  blogContent,  string  blogAuthor);
    }
}

 

 

如果我们想在我们的这个WCF中使用Sharepoint 对象模型,我们必须为项目添加引用Microsoft.SharePoint.dll,以下是Service代码:

 

ExpandedBlockStart.gif 代码
using  System;
using  System.Collections.Generic;
using  System.Linq;
using  System.Runtime.Serialization;
using  System.ServiceModel;
using  System.ServiceModel.Web;
using  System.Text;

using  Microsoft.SharePoint;

namespace  UpdateSPList
{
    
//  NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "Service1" in code, svc and config file together.
     public   class  Service1 : IService1
    {
        
public   void  updateBlog( string  SPSite,  string  blogTitle,  string  blogContent,  string  blogAuthor)
        {
string  strDashListRoot  =  SPSite;
            
using  (SPSite site  =   new  SPSite(strDashListRoot))
            {
                
using  (SPWeb web  =  site.OpenWeb())
                {
                    web.AllowUnsafeUpdates 
=   true ;
                    SPList list 
=  web.Lists[ " Blog " ];
                    SPListItem Item 
=  list.Items.Add();
                    Item[
" Title " =  blogTitle;
                    Item[
" Content " =  blogContent;
                    Item[
" Writer " =  blogAuthor;
                    Item.Update();
                    web.AllowUnsafeUpdates 
=   false ;
                }
            }
        }
    }
}

 

 

发布WCF 服务。

下面创建一个客户端的WPF应用来调用这个WCF服务,创建WPF应用的过程略。

创建好WPF项目后,我们添加Service Reference,把刚创建的WCF服务地址填上

在添加按钮的方法内添加如下代码:

 

ExpandedBlockStart.gif 代码
private   void  btnAdd_Click( object  sender, RoutedEventArgs e)
        {
            strSPURL 
=  txtbxURL.Text;
            strBlogTitle 
=  txtbxBlogTitle.Text;
            strBlogContent 
=  txtbxBlogContent.Text;
            strBlogAuthor 
=  txtbxBlogAuthor.Text;
            WCFUpdateList.Service1Client myWCFProxy 
=   new  WCFUpdateList.Service1Client();
            myWCFProxy.updateBlog(strSPURL, strBlogTitle, strBlogContent, strBlogAuthor);
            myWCFProxy.Close();
        }

 

 

当然,不要忘记给类添加如下的变量:

string strSPURL = "";

string strBlogTitle = "";

string strBlogContent = "";

string strBlogAuthor = "";

 

运行项目,填写一些数据,提交后你会发现你提交的数据被记录到列表中了。

 

异常:

如果在调用WCF的过程中发生异常,想要查看异常信息时,你需要在WCFweb.config中添加如下黑体的代码:

 

ExpandedBlockStart.gif 代码
< behaviors >
      
< serviceBehaviors >
        
< behavior  name ="UpdateSPList.SPListServiceBehavior" >
          
< serviceMetadata  httpGetEnabled ="true"   />
          
< serviceDebug  includeExceptionDetailInFaults ="true"   />
        
</ behavior >
      
</ serviceBehaviors >
    
</ behaviors >

 

 在调用WCF的过程中,如果抛出如下异常信息

Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

或者:

ExpandedBlockStart.gif 代码
The Web application at http://XXXX:8080 could not be found. Verify that you have typed the URL correctly. If the URL should be serving existing content, the system administrator may need to add a new request URL mapping to the intended application.

 

 

解决方法:

1)      打开IIS,定位到Application Pools,找到你部署的WCF站点对应的Application Pool,右键单击选择“高级”,弹出如下对话框

 

2)      点击Identity右边的按钮,在弹出的对话框中

选择Custom account,点击Set按钮,弹出对话框

填写一个有权限访问你的MOSS站点的账户和密码,一路OK下去。

再次运行调用WCFWPF项目,异常不再显示。

 项目文件:http://files.cnblogs.com/Jayan/WCFList.zip

 

 

转载于:https://www.cnblogs.com/Jayan/archive/2010/07/29/1787788.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值