现在的位置: 首页 > 综合 > 正文

Update SPListItem using Web Service

2013年03月03日 ⁄ 综合 ⁄ 共 7692字 ⁄ 字号 评论关闭

这几天为了在SharePoint上集成发短信的功能,忙得不可开交。短信猫的驱动程序Mondem.dll在64跨平台调用的时候出现封装如下:

public  class  CmonDem 
    
{
        
            
//初始化
        [DllImport("mondem.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            
public static extern int fnInitModem(int nPortNo);
           
//得到短信猫的类型
        [DllImport("mondem.dll", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
            
public static extern int fnSetModemType(int ComNo,int modemType);


            
//发送
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnSendMsg (int nPortNo,  string  szPhone,  string  szMsg);
            
//目标手机号码+序列号+已发送次数   例如:  138123456789,001,0

            
//接收
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnReadMsgEx (int nPortNo,    StringBuilder  szHeader,    StringBuilder  szReadMsg);
         
            
//关闭
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnCloseModem (int nPortNo);

            
//得到软件最大支持端口号
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnGetPortMax();

            
//获取端口状态函数 
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnGetStatus(int nPortNo);

            
//取发送缓冲区条数
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnGetSndCount (int nPortNo);

            
//取接收缓冲区条数
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnGetRecCount (int nPortNo);

            
//清除发送缓冲区
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnClrSndBuf (int nPortNo);

            
//清除接收缓冲区
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnClrRecBuf (int nPortNo);


            
//检测软件狗存在
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnDogCheck ();

            [DllImport(
"mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnDogSerial();

            
//读取软件狗信息
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnDogRead (int bufLen,  string  szBuf);
   
            
//写软件狗信息
            [DllImport("mondem.dll", CharSet=CharSet.Ansi)]
            
public static extern int fnDogWrite (int bufLen, string  szBuf);

            [DllImport(
"kernel32.dll")]
            
public static extern int GetTickCount();
            

     

        }

但是初始化的时候出现如下错误:

System.BadImageFormatException was unhandled
  Message
="试图加载格式不正确的程序。 (异常来自 HRESULT:0x8007000B)"
  Source
="dxmwinapp"
  StackTrace:
       在 dxmwinapp.CmonDem.fnGetStatus(Int32 nPortNo)
       在 dxmwinapp.Form1.timer1_Tick(Object sender, EventArgs e) 位置 E:\短信猫1\短信猫\二次开发包\DLL接口\samples\c#Demo(news)\Form1.cs:行号 
446
       在 System.Windows.Forms.Timer.OnTick(EventArgs e)
       在 System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message
& m)
       在 System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       在 System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG
& msg)
       在 System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
       在 System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
       在 dxmwinapp.Form1.Main() 位置 E:\短信猫1\短信猫\二次开发包\DLL接口\samples\c#Demo(news)\Form1.cs:行号 
349
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()

查了好久的资料,才弄明天原来是编译的时候出问题了,Mondem.dll驱动是在32位机器上编译的,但是我的程序是编译成64位的,所以平台调用的时候就出错误了,
因此需要选择X86的目标机器编译才不会有问题,具体设置如下图所示: 

可事情还没完,选择X86的程序后,此程序却访问不了SharePoint 站点了,代码片断如下:

 using (SPSite site = new SPSite("http://www.test.com"))
                  
{
                      
using (SPWeb targetWeb = site.AllWebs[""])
                      
{
                          site.AllowUnsafeUpdates 
= true;
                          targetWeb.AllowUnsafeUpdates 
= true;

                          
                         

                         
                          targetWeb.AllowUnsafeUpdates 
= false;
                          site.AllowUnsafeUpdates 
= false;
                      }

                  }

初始化SPSite的时候就出现异常,异常信息如下:

System.IO.FileNotFoundException was unhandled
  Message
="找不到位于 http://www.test.com 的 Web 应用程序。请确认正确键入了此 URL。如果此 URL 需要提供现有内容,则系统管理员可能需要添加到指定应用程序的新请求 URL 映射。"
  Source
="Microsoft.SharePoint"
  StackTrace:
       在 Microsoft.SharePoint.SPSite..ctor(SPFarm farm, Uri requestUri, Boolean contextSite, SPUserToken userToken)
       在 Microsoft.SharePoint.SPSite..ctor(String requestUrl)
       在 ConsoleApplication3.Program.Validate() 位置 e:\Visual Studio 
2005\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:行号 79
       在 ConsoleApplication3.Program.Main(String[] args) 位置 e:\Visual Studio 
2005\Projects\ConsoleApplication3\ConsoleApplication3\Program.cs:行号 21
       在 System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
       在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       在 System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       在 System.Threading.ThreadHelper.ThreadStart()

莫名其妙,但是可以肯定地是由于编译平台X86的原因,于是此路不通,只得在SharPoint里面调用Web Service对列表字段进行操作,也深刻的体会到了Web Service的妙处,同时意识到平时自己做的Web Service是多么的不规范,调用,或者发布Web Service,都只能使用XML,这才是通用的,否则就失去意义,通过Web Service 对SharePoint 列表的操作代码如下,里面有详细的注释,应该没什么问题

  /*初始化 Web service.*/
            com.test.www.Lists listService 
= new com.test.www.Lists();

            
/*认证*/
            listService.Credentials 
=
                System.Net.CredentialCache.DefaultCredentials;



            
/*设置Web Service的Url属性.*/
            listService.Url 
=
                
"http://www.test.com/_vti_bin/Lists.asmx";

            
/*使用XML 传递参数*/
            System.Xml.XmlDocument doc 
= new System.Xml.XmlDocument();
            System.Xml.XmlElement batchElement 
= doc.CreateElement("Batch");
            batchElement.SetAttribute(
"OnError""Continue");
            batchElement.SetAttribute(
"ListVersion""1");
            
/*需要操作视图的GUID*/
            batchElement.SetAttribute(
"所有项目",
                
"{38c8fdc3-4259-4d82-a2b5-d907d11a5a7c}");/*GUID*/

            
/*这段CAML需要填写正确,Web Service调用的就是根据它执行相应操作,CAML语法还是挺强大的,
             * 微软不知道用了多少人研究出来,它可以处理各种逻辑关系
*/

            batchElement.InnerXml 
= "<Method ID='1' Cmd='Update'>" +//此ID是Method的执行顺序
                                    "<Field Name='ID'>6</Field>" + //此ID是SPListItem的ID,更新或者删除的时候才有用
                                    "<Field Name='Title'>sixth</Field></Method>" + //需要更新的字段及其值
                                    "<Method ID='2' Cmd='New'>" +//此ID是Method的执行顺序
                                    "<Field Name='ID'>7</Field>" +//此ID是SPListItem的ID,新建的时候此值无效,可以不填
                                    "<Field Name='Title'>seventh</Field></Method>";//新建时字段的值

            
//Update list items.
            listService.UpdateListItems("Test", batchElement);

到此,问题基本上解决了

【上篇】
【下篇】

抱歉!评论已关闭.