sharepoint2010应用开发一:使用服务端对象模型(ServerSide 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();
        }
代码
private void btnClear_Click(object sender, RoutedEventArgs e)
        {
            txtbxListName.Text 
= "";
            txtbxSPURL.Text 
= "";
            txtbxBlogTitle.Text 
= "";
            txtbxBlogContent.Text 
= "";
            txtbxBlogAuthor.Text 
= "";
        }
代码
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、 可能你在运行的时候回出现这样的一场信息

代码
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即可,如图:

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

原文地址:https://www.cnblogs.com/Jayan/p/1780385.html