SharePoint Web Service系列:进行列表的增删改

异构应用访问SharePoint的文档库或列表时,使用WebService的方式再恰当不过了。有朋友问我如何在dotNet Framework 3.0下的应用程序中控制SharePoint 2003中的列表项。想一想类似的场景应该比较常见,所以写了下面的demo。以下的代码在VS2005中测试通过。

using System;
using System.Xml;
using System.Collections.Generic;
using System.Text;

namespace ConsoleDemo
{
    
class Program
    {
        
static void Main(string[] args)
        {

            
try 
            {
                test();
            }
            
catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
            Console.WriteLine(
"END");
            Console.Read();
        }

        
private static void test()
        {
            LabPortal.Lists listService 
= new ConsoleDemo.LabPortal.Lists();
            listService.Credentials 
= System.Net.CredentialCache.DefaultCredentials;

            
string strBatch = "<Method ID='1' Cmd='New'>"+  //
                                                            
//ID是Method的唯一标识,如果有多个Method就都列在下面
                                                            
//Cmd的类型有:New,Update,Delete。分别对应增加,删除,更新
                "<Field Name='ID'>New</Field>"+             //
                                                            
//ID在增加操作时只是个唯一标记,可以随便指定,并不对应到实际
                                                            
//listitem的ID。但在删除和更新时就必须是实际的ID了,因为要靠这个来唯一指定一条记录
                "<Field Name='Title'>Smf</Field>"+
                
"</Method>";
            XmlDocument xmlDoc 
= new XmlDocument();
            XmlElement elBatch 
= xmlDoc.CreateElement("Batch");
            
//Batch元素下面的这些Attribue是可选的
            elBatch.SetAttribute("OnError""Continue");    //指定出错后是返回还是继续下一步
            elBatch.SetAttribute("ListVersion","1");    //指定列表的版本
            elBatch.SetAttribute("ViewName""654446D3-8E70-4483-B2B6-F87329EAC2D9");  //指定所操作的列表视图GUID
            elBatch.InnerXml = strBatch;
            XmlNode ndReturn 
= listService.UpdateListItems("Contracts", elBatch);  //在名为Contracts的联系人列表中增加一条记录
            
            Console.WriteLine(ndReturn.OuterXml);
        }
    }
}
posted @ 2006-10-11 18:47  Sunmoonfire  阅读(4830)  评论(5编辑  收藏  举报