Note that FileRef cannot use encoded URL.
For the GetDocID method can refer to this post.
private static void DeleteFile(string FilePath)
{
try
{
using (ListWS.Lists listService = new ListWS.Lists())
{
listService.Url = ListURL;
listService.Credentials = System.Net.CredentialCache.DefaultCredentials;
listService.Credentials = new System.Net.NetworkCredential(username, password);
//Get the file id
string fileName = FilePath.Substring(FilePath.LastIndexOf("/") + 1);
string DocID = GetDocID(fileName);
//get guid
string lListName = "EDMS";
XmlNode lSharePointListName = listService.GetList(lListName);
string lListID = lSharePointListName.Attributes["ID"].Value;
XmlDocument doc = new XmlDocument();
//Perform deletion
string xmlCommand;
doc = new XmlDocument();
//ID is the ID field value of MyFolder element
xmlCommand = "<Method ID='1' Cmd='Delete'><Field Name='ID'>" + DocID + "</Field><Field Name='FileRef'>" +
FilePath.Replace("%20"," ") + "</Field></Method>";
XmlElement ele = doc.CreateElement("Batch");
ele.SetAttribute("OnError", "Continue");
ele.SetAttribute("ListVersion", "1");
//ele.SetAttribute("RootFolder", FilePath.Substring(0, FilePath.LastIndexOf("/")));
ele.InnerXml = xmlCommand;
XmlNode node1 = listService.UpdateListItems(lListID, ele);
if ((node1 != null) && (node1.InnerText == SUCCESS))
{
Console.WriteLine("SUCCESS");
}
else
{
Console.WriteLine("There is an error : " + node1.InnerText);
}
}
}
catch (Exception ex)
{
throw ex;
}
}
here is the error that you may get:
1) 0x81020030Invalid file name.\n\nThe file name you specified could not be used. It may be the name of an existing file or directory, or you may not have permission to access the file.
This is because FileRef must be included in the query for deleting the item.
2) You got success code (0x00000000) however the file just stays there in the Document Library.
This is because the FileRef path is using encoded URL
(eg. http://url/a/b/c/cc%20c.pdf -> must change to -> http://url/a/b/c/cc c.pdf)
After changing to non-encoded URL, wala.. it's working like magic!
Hope it helps!
No comments:
Post a Comment