C# 如何使用Web服务更新Sharepoint上上载文档的属性?

C# 如何使用Web服务更新Sharepoint上上载文档的属性?,c#,.net,xml,sharepoint-2007,C#,.net,Xml,Sharepoint 2007,我正在尝试更新/编辑Sharepoint 2007上上载文档的属性 我的代码: Lists listService = new Lists(); listService.PreAuthenticate = true; listService.Credentials = new NetworkCredential(username,password); listService.Url = "http://myserver/SiteName/_vti_bin/lists.asmx"; s

我正在尝试更新/编辑Sharepoint 2007上上载文档的属性

我的代码:

 Lists listService = new Lists();
 listService.PreAuthenticate = true;
 listService.Credentials = new NetworkCredential(username,password);
 listService.Url = "http://myserver/SiteName/_vti_bin/lists.asmx";

 string strBatch =

                   "<Method ID='1' Cmd='Update'> "
                   + " <Field Name='ID'>3</Field> "
                   + " <Field Name='Name'>Preeti</Field> "                 
                   + " </Method> ";

 XmlDocument xmlDoc = new System.Xml.XmlDocument();
 System.Xml.XmlElement elBatch = xmlDoc.CreateElement("Batch");
 elBatch.SetAttribute("OnError", "Continue");

elBatch.SetAttributeNode("UserName", "Preeti");
elBatch.InnerXml = strBatch;
XmlNode ndReturn = listService.UpdateListItems(ListName, elBatch);

MessageBox.Show(ndReturn.OuterXml); 
listslistservice=新列表();
listService.PreAuthenticate=true;
凭证=新的网络凭证(用户名、密码);
listService.Url=”http://myserver/SiteName/_vti_bin/lists.asmx";
串珠=
" "
+ " 3 "
+“普里蒂”
+ "  ";
XmlDocument xmlDoc=new System.Xml.XmlDocument();
System.Xml.xmlement elBatch=xmlDoc.CreateElement(“批处理”);
elBatch.SetAttribute(“OnError”、“Continue”);
elBatch.SetAttributeNode(“用户名”、“Preeti”);
elBatch.InnerXml=strBatch;
XmlNode ndReturn=listService.UpdateListItems(ListName,elBatch);
Show(ndReturn.OuterXml);
参考


获取错误:“一个或多个字段类型未正确安装。请转到列表设置页面删除这些字段”。

检查以确保使用的是字段的内部名称

要获取字段的内部名称,请打开“新建”表单并从上下文菜单中单击“查看源代码”(右键单击“新建”表单页面中的任意位置以打开上下文菜单)。您将在源文件末尾附近看到字段及其内部名称

看看代码:

string strBatch =

               "<Method ID='1' Cmd='Update'> "
               + " <Field Name='ID'>3</Field> "
               + " <Field Name='Name'>Preeti</Field> "                 
               + " </Method> ";

sharepoint中是否有这两个单独的属性?

提供了以下解决方案:

但是,请注意,如另一个答案中所述,字段的内部名称是必需的

Web.Config键

如果选择,则将以下内容添加到应用程序的Web.config中(仅适用于此示例,或者您可以在代码中简单地包括所需的值[服务器、文档库、用户、域、密码等]):


代码:

Public Function WSSUpdateFile(ByVal sFileName As String, ByVal sSiteDoc As String, ByVal sTestCol As String) As String

        Dim sUser As String = ConfigurationManager.AppSettings("User")
        Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
        Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
        Dim sFileIDinList As String
        Dim strBatch As String = ""
        sSiteDoc = Replace(sSiteDoc, "%20", " ")
        sSiteDoc = Replace(sSiteDoc, "\", "/")
        Dim sFinalFilePath As String
        Dim sSPURL As String = ConfigurationManager.AppSettings("SharePointServer")
        Dim sDocLib As String = ConfigurationManager.AppSettings("DocLibrary")
        Try
            Dim netAccess As System.Net.NetworkCredential = New System.Net.NetworkCredential(sUser, sPwd, sDomain)
            Dim listService As New SPLists.Lists
            listService.Url = sSPURL & "/_vti_bin/lists.asmx"
            listService.Credentials = netAccess
            sFileIDinList = sGetID(listService.Url, sDocLib, sFileName)
            If sFileIDinList <> "" Then
                sFinalFilePath = sSPURL & "/" & sDocLib & "/" & sFileName
                'Now we have FileID so update the list
                strBatch = "<Method ID='1' Cmd='Update'>" + _
                    "<Field Name = 'ID'>" & sFileIDinList & "</Field>" + _
                    "<Field Name = 'FileRef'>" & sFinalFilePath & "</Field>" + _
                    "<Field Name = 'TestCol'>" & sTestCol & "</Field>" + _
                    "</Method>"
                Dim xmlDoc = New System.Xml.XmlDocument
                Dim elBatch As System.Xml.XmlElement = xmlDoc.createelement("Batch")
                elBatch.InnerXml = strBatch
                Dim ndreturn As System.Xml.XmlNode = listService.UpdateListItems(sDocLib, elBatch)
            End If
            Return "TRUE"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Private Function sGetID(ByVal sURL As String, ByVal sListGUID As String, ByVal sFileName As String) As String
        Dim sUser As String = ConfigurationManager.AppSettings("User")
        Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
        Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
        Dim netAccess As System.Net.NetworkCredential = New System.Net.NetworkCredential(sUser, sPwd, sDomain)
        Dim L As New SPLists.Lists
        L.Credentials = netAccess
        L.Url = sURL
        Dim xmldoc As XmlDocument = New XmlDocument
        Dim query As XmlNode = xmldoc.CreateNode(XmlNodeType.Element, "Query", "")
        query.InnerXml = "<OrderBy><FieldRef Name='Modified'  Ascending='False'></FieldRef></OrderBy>"""
        Try
            Dim caml As XmlNode = L.GetListItems(sListGUID, Nothing, query, Nothing, "1", Nothing)
            Dim id As String = caml.ChildNodes(1).ChildNodes(1).Attributes("ows_ID").Value
            Return id
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function
公共函数WSSUpdateFile(ByVal sFileName作为字符串,ByVal sSiteDoc作为字符串,ByVal sTestCol作为字符串)作为字符串
Dim sUser As String=ConfigurationManager.AppSettings(“用户”)
Dim sPwd As String=ConfigurationManager.AppSettings(“Pwd”)
Dim sDomain As String=ConfigurationManager.AppSettings(“域”)
将SFiledList设置为字符串
Dim strBatch As String=“”
sSiteDoc=替换(sSiteDoc、%20、“”)
sSiteDoc=替换(sSiteDoc,“\”,“/”)
将sFinalFilePath设置为字符串
Dim sSPURL As String=ConfigurationManager.AppSettings(“SharePointServer”)
Dim sDocLib As String=ConfigurationManager.AppSettings(“DocLibrary”)
尝试
Dim netAccess As System.Net.NetworkCredential=新的System.Net.NetworkCredential(sUser、sPwd、sDomain)
Dim listService作为新的SPLists.List
listService.Url=sSPURL&“/\u vti\u bin/lists.asmx”
listService.Credentials=netAccess
sFileIDinList=sGetID(listService.Url、sDocLib、sFileName)
如果为列表“”,则
sFinalFilePath=sSPURL&“/”&sDocLib&“/”&sFileName
'现在我们有了FileID,所以请更新列表
strBatch=”“+_
“”&sfileidList&“”+_
“”&sFinalFilePath&“”+_
&sTestCol&+_
""
Dim xmlDoc=新System.Xml.XmlDocument
Dim elBatch As System.Xml.xmlement=xmlDoc.createelement(“批处理”)
elBatch.InnerXml=strBatch
Dim ndreturn As System.Xml.XmlNode=listService.UpdateListItems(sDocLib,elBatch)
如果结束
返回“TRUE”
特例
回信
结束尝试
端函数
私有函数sGetID(ByVal sURL作为字符串,ByVal sListGUID作为字符串,ByVal sFileName作为字符串)作为字符串
Dim sUser As String=ConfigurationManager.AppSettings(“用户”)
Dim sPwd As String=ConfigurationManager.AppSettings(“Pwd”)
Dim sDomain As String=ConfigurationManager.AppSettings(“域”)
Dim netAccess As System.Net.NetworkCredential=新的System.Net.NetworkCredential(sUser、sPwd、sDomain)
将L变暗为新的列表。列表
L.凭据=netAccess
L.Url=sURL
Dim xmldoc As XmlDocument=新的XmlDocument
Dim查询为XmlNode=xmldoc.CreateNode(XmlNodeType.Element,“query”,“”)
query.InnerXml=“”
尝试
Dim caml As XmlNode=L.GetListItems(sListGUID,Nothing,query,Nothing,“1”,Nothing)
作为字符串的Dim id=caml.ChildNodes(1).ChildNodes(1).Attributes(“ows_id”).Value
返回id
特例
回信
结束尝试
端函数

尝试使用U2UCamlCreator工具测试sharepoint查询。
此工具将帮助您生成批以更新sharepoint字段和许多其他功能

<configuration>
<appSettings>

<add key="SharePointServer" value=http://SP Portal/Site/>
<add key="DocLibrary" value="Doclib"/>
<add key="User" value="User"/>
<add key="Domain" value="Domain"/>
<add key="Pwd" value="Pwd"/>
<add key="GlobalSharedPath" value="D:\"/>
</appSettings>
Public Function WSSUpdateFile(ByVal sFileName As String, ByVal sSiteDoc As String, ByVal sTestCol As String) As String

        Dim sUser As String = ConfigurationManager.AppSettings("User")
        Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
        Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
        Dim sFileIDinList As String
        Dim strBatch As String = ""
        sSiteDoc = Replace(sSiteDoc, "%20", " ")
        sSiteDoc = Replace(sSiteDoc, "\", "/")
        Dim sFinalFilePath As String
        Dim sSPURL As String = ConfigurationManager.AppSettings("SharePointServer")
        Dim sDocLib As String = ConfigurationManager.AppSettings("DocLibrary")
        Try
            Dim netAccess As System.Net.NetworkCredential = New System.Net.NetworkCredential(sUser, sPwd, sDomain)
            Dim listService As New SPLists.Lists
            listService.Url = sSPURL & "/_vti_bin/lists.asmx"
            listService.Credentials = netAccess
            sFileIDinList = sGetID(listService.Url, sDocLib, sFileName)
            If sFileIDinList <> "" Then
                sFinalFilePath = sSPURL & "/" & sDocLib & "/" & sFileName
                'Now we have FileID so update the list
                strBatch = "<Method ID='1' Cmd='Update'>" + _
                    "<Field Name = 'ID'>" & sFileIDinList & "</Field>" + _
                    "<Field Name = 'FileRef'>" & sFinalFilePath & "</Field>" + _
                    "<Field Name = 'TestCol'>" & sTestCol & "</Field>" + _
                    "</Method>"
                Dim xmlDoc = New System.Xml.XmlDocument
                Dim elBatch As System.Xml.XmlElement = xmlDoc.createelement("Batch")
                elBatch.InnerXml = strBatch
                Dim ndreturn As System.Xml.XmlNode = listService.UpdateListItems(sDocLib, elBatch)
            End If
            Return "TRUE"
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function

Private Function sGetID(ByVal sURL As String, ByVal sListGUID As String, ByVal sFileName As String) As String
        Dim sUser As String = ConfigurationManager.AppSettings("User")
        Dim sPwd As String = ConfigurationManager.AppSettings("Pwd")
        Dim sDomain As String = ConfigurationManager.AppSettings("Domain")
        Dim netAccess As System.Net.NetworkCredential = New System.Net.NetworkCredential(sUser, sPwd, sDomain)
        Dim L As New SPLists.Lists
        L.Credentials = netAccess
        L.Url = sURL
        Dim xmldoc As XmlDocument = New XmlDocument
        Dim query As XmlNode = xmldoc.CreateNode(XmlNodeType.Element, "Query", "")
        query.InnerXml = "<OrderBy><FieldRef Name='Modified'  Ascending='False'></FieldRef></OrderBy>"""
        Try
            Dim caml As XmlNode = L.GetListItems(sListGUID, Nothing, query, Nothing, "1", Nothing)
            Dim id As String = caml.ChildNodes(1).ChildNodes(1).Attributes("ows_ID").Value
            Return id
        Catch ex As Exception
            Return ex.Message
        End Try
    End Function