【问题标题】:How to update Properties of Uploaded Documents on Sharepoint using Web Services?如何使用 Web 服务更新 Sharepoint 上上传文档的属性?
【发布时间】:2010-03-05 10:12:43
【问题描述】:

我正在尝试在 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); 

引用Link

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

【问题讨论】:

    标签: c# .net xml sharepoint-2007


    【解决方案1】:

    以下解决方案来自:http://www.codeproject.com/KB/sharepoint/File_Shunter.aspx

    但是请注意,正如另一个答案中提到的,该字段的内部名称是必需的。

    Web.Config 键

    如果您选择,将以下内容添加到您的应用程序的 Web.config(仅适用于本示例,或者您可以简单地在你的代码):

    <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
    

    【讨论】:

      【解决方案2】:

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

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

      查看代码:

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

      ...

      elBatch.SetAttributeNode("UserName", "Preeti");
      

      这实际上是 sharepoint 中的两个独立属性吗?

      【讨论】:

        【解决方案3】:

        尝试使用 U2UCamlCreator 工具来测试您的共享点查询。
        此工具将帮助您构建批次以更新共享点字段和许多其他功能。

        【讨论】:

          猜你喜欢
          • 2013-09-21
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-11-16
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多