sharepoint2010应用开发一:使用服务端对象模型(Server-Side Object Model)操作列表

最近准备把sharepoint2007升级到sharepoint2010,看了许多学习资料,为了以后查找方便,记录下自己的学习过程吧!希望能和大家分享下,共同研究sharepoint.

sharepoint2010中,你可以通过以下几种方式对列表(List)进行操作:

1.       在服务器上使用Microsoft.SharePoint命名空间(服务端对象模型)来访问列表。这就意味着你创建的任何应用必须在服务端才能正常访问。

2.       使用列表web service

3.       自定义一个寄宿在IIS上的ASP.NET  Web Service,你也可以把你创建的Web Service发布到Sharepoint的文件系统里

4.       创建一个自定义的WCF 服务发布到IIS或者Sharepoint站点的跟目录。

5.       使用Sharepoint客户对象模型(Client Object Model),它可以在远程机器上对Sharepoint的列表进行编程,支持Silverlight,.NET,Javascript

一、使用服务端对象模型(Server-Side Object Model)操作列表

首先需要在你的Sharepoint站点中创建一个 List,如Blog,再为这个List添加两个字段,如:Content, Writer,作为学习,这两个字段使用单行文本即可。

1、 打开VSFile->Project->New->WPF Application,确保选择.NET Framework 3.5,否则你将碰到编译问题。

2、 为项目起个名字,如:WPFSPServerApp

3、 添加引用:Microsoft.Sharepoint.dll,一般在路径:c:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\ISAPI.

4、 MainWindow.XAML的设计视图下,为它添加5Label5textbox3个按钮,属性如下:

Label lblSPUrl, lblListName, lblBlogTitle, lblBlogContent,lblBlogAuthor

Textbox txtbxSPURL,txtbxListName, txtbxBlogTitle, txtbxBlogContent,txtbxBlogAuthor

Button btnLoad, btnClear, btnExit

最终效果如下图:

 

5、 分别双击加载、清空、退出按钮,并分别添加如下代码:

别忘记给MainWindow.xaml.cs添加using Microsoft.SharePoint,并添加如下5个变量:

 

string  strSPSiteURL  =   "" ;
string  strSPListName  =   "" ;
string  strBlogTitle  =   "" ;

 

 

private   void  btnExit_Click( object  sender, RoutedEventArgs e)
        {
            Application.Current.Shutdown();
        }

 

 

ExpandedBlockStart.gif 代码
private   void  btnClear_Click( object  sender, RoutedEventArgs e)
        {
            txtbxListName.Text 
=   "" ;
            txtbxSPURL.Text 
=   "" ;
            txtbxBlogTitle.Text 
=   "" ;
            txtbxBlogContent.Text 
=   "" ;
            txtbxBlogAuthor.Text 
=   "" ;
        }

 

 

ExpandedBlockStart.gif 代码
private   void  btnLoad_Click( object  sender, RoutedEventArgs e)
        {
            strSPSiteURL 
=  txtbxSPURL.Text;
            strSPListName 
=  txtbxListName.Text;
            strBlogTitle 
=  txtbxBlogTitle.Text;
            strBlogContent 
=  txtbxBlogContent.Text;
            strBlogAuthor 
=  txtbxBlogAuthor.Text;
            
using  (SPSite site  =   new  SPSite(strSPSiteURL))
            {
                
using  (SPWeb web  =  site.OpenWeb())
                {
                    web.AllowUnsafeUpdates 
=   true ;
                    SPList list 
=  web.Lists[strSPListName];
                    SPListItem Item 
=  list.Items.Add();
                    Item[
" Title " =  strBlogTitle;
                    Item[
" Content " =  strBlogContent;
                    Item[
" Writer " =  strBlogAuthor;
                    Item.Update();
                    web.AllowUnsafeUpdates 
=   false ;
                }
            }
        }

 

 

6、 F5运行,在弹出的界面中输入一些信息,如:

 

 

7、 打开你的站点,到Blog列表,你会看到你刚刚添加的一条数据

 

8、 可能你在运行的时候回出现这样的一场信息

 

ExpandedBlockStart.gif 代码
The Web application at http: // anjie: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.

 

 

不要担心,这是因为你的目标平台默认是X86的原因,你只要到项目属性中的Build选项,把目标平台修改为X64或者Any CPU即可,如图:

完整项目文件下载:http://files.cnblogs.com/Jayan/WPFSPServerApp.zip

转载于:https://www.cnblogs.com/Jayan/archive/2010/07/18/1780385.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值