如何使用UpdateListItems更新sharepoint列表的文档

时间:2009-08-18 09:55:20

标签: sharepoint sharepoint-2007

我可以使用这段代码删除列表中的文档:

// string fileRef = "folder/myplan.doc"
private void DeleteDocument(string fileRef, string listGuid)
{
    string strBatch = "<Method ID=\"1\" Cmd=\"Delete\">" +
        "<Field Name=\"ID\">1</Field>" +
        "<Field Name=\"FileRef\">" + fileRef + "</Field>" +
        "</Method>";

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

    try
    {
        XmlNode ndReturn = lists.UpdateListItems(listGuid, elBatch);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

如您所见,我必须指定fileRef,这将告诉sharepoint我想删除哪个文件。

但是,我无法以相同的方式更新列表的文档名称。没有返回错误,名称不会被更改。

“列表”Web服务的凭据非常正确。这就是我可以保证的。 fileRef中指定的文档也是正确的。

private void UpdateDocument(string fileRef, string listGuid, string newName)
{
    string strBatch = "<Method ID=\"1\" Cmd=\"Update\">" +
        "<Field Name=\"ID\">1</Field>" +
        "<Field Name=\"Title\">" + newName + "</Field>" +
        "<Field Name=\"FileRef\">" + fileRef + "</Field>" +
        "</Method>";

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

    try
    {
        XmlNode ndReturn = lists.UpdateListItems(listGuid, elBatch);
    }
    catch (Exception ex)
    {
        throw ex;
    }
}

我很好奇这里的示例:http://msdn.microsoft.com/en-us/library/lists.lists.updatelistitems.aspx因为我没有看到指定文档(guid或fileRef)的位置,无论是更新还是删除。 sharepoint如何知道应删除/更新哪个文档?

我做错了什么或误解了什么?

任何人都能成功更新文档,请分享您的工作。 感谢

2 个答案:

答案 0 :(得分:0)

我认为你应该使用BaseName而不是Title

string strBatch = "<Method ID=\"1\" Cmd=\"Update\">" + 
    "<Field Name=\"ID\">1</Field>" + 
    "<Field Name=\"BaseName\">" + newName + "</Field>" + 
    "<Field Name=\"FileRef\">" + fileRef + "</Field>" + 
    "</Method>"; 

答案 1 :(得分:0)

"<Field Name=\"ID\">1</Field>" +

您正在设置ID为1

的项目的属性
相关问题