Second Life of a Hungarian SharePoint Geek

September 21, 2009

Deleting documents or folders using the built-in web services

Filed under: Code, SharePoint, Web service — Tags: , , — Peter Holpar @ 03:39

Deleting documents or folders using the built-in Lists web service is not trivial. To call the UpdateListItems method to do this you need to pass the ID of the document (or folder) and the FileRef property of the document (or folder). If you don’t know the latter one, you have to get it first, for example, using the GetListItems method.

I wrapped this functionality into a single method, that assumes you have a member instance for the Lists web service called ListService, and accepts three parameters: the list name, the ID of the item and the FileRef of the item. If the FileRef is empty, then the method tries to get it from the server.

The method returns the error code as an integer.

Here comes the code, I hope it will help somebody to do the deletion in SharePoint document libraries:


private int? WSDelete(String listName, int id, String fileRef)
{
// in this example we use the default
credentials
ListService.Credentials =
CredentialCache.DefaultCredentials;
// if fileRef is empty then we need further information
if (String.IsNullOrEmpty(fileRef))
{

// get the document / folder with
the specified ID
XmlDocument
xmlQuery = new XmlDocument();

xmlQuery.LoadXml(String.Format("<Query><Where><Eq><FieldRef
Name='ID' /><Value
Type='Counter'>{0}</Value></Eq></Where></Query>"
,
id));
// we need the ID and the name
(FileRef)
XmlDocument
xmlViewFields = new XmlDocument();

xmlViewFields.LoadXml("<ViewFields><FieldRef Name='ID'
/><FieldRef Name='FileRef' /></ViewFields>"
);
// check the documents / folders
in subfolders too
XmlDocument
xmlQueryOptions = new
XmlDocument();

xmlQueryOptions.LoadXml("<QueryOptions><ViewAttributes
Scope='RecursiveAll' /></QueryOptions>"
);
XmlNode getResult =
ListService.GetListItems(listName, null, xmlQuery, xmlViewFields, null,
xmlQueryOptions, null);
XmlNamespaceManager nsmgr = new
XmlNamespaceManager(getResult.OwnerDocument.NameTable);

nsmgr.AddNamespace("z",
"#RowsetSchema");

nsmgr.AddNamespace("rs", "urn:schemas-microsoft-com:rowset");
XmlNode getNode =
getResult.SelectSingleNode("rs:data/z:row", nsmgr);
if (getNode !=
null)

{

XmlAttribute fileRefAttr =
getNode.Attributes["ows_FileRef"];

// should not be null, but we check
it
if
(fileRefAttr !=
null)

{

fileRef =
fileRefAttr.Value;

int pos =
fileRef.IndexOf(";#");

if (pos >
-1)

{

fileRef = fileRef.Substring(pos +
2);

}

}
}
}
// build the delete request based on the information we
have
XmlDocument deleteRequest = new XmlDocument();
String deleteBatch = String.Format("<Batch
OnError='Continue'>"

+

"<Method ID='1' Cmd='Delete'>"
+

"<Field Name='ID'>{0}</Field>"
+

"<Field Name='FileRef'>{1}</Field>"
+

"</Method>"
+

"</Batch>", id, fileRef);
deleteRequest.LoadXml(deleteBatch);
XmlNode deleteResult = ListService.UpdateListItems("Shared
Documents"
, deleteRequest.DocumentElement);
XmlNamespaceManager nsmgr2 = new
XmlNamespaceManager(deleteResult.OwnerDocument.NameTable);

nsmgr2.AddNamespace("default", "http://schemas.microsoft.com/sharepoint/soap/");

int? errorCode = null;
XmlNode errorCodeNode =
deleteResult.SelectSingleNode("default:Result/default:ErrorCode",
nsmgr2);
if (errorCodeNode != null)

{
String errorCodeString =
errorCodeNode.InnerText.ToLower();

int hexPos =
errorCodeString.IndexOf("0x");
if
(hexPos > -1)

{

errorCodeString = errorCodeString.Substring(hexPos +
2);

}
errorCode =
Int32.Parse(errorCodeString,
NumberStyles.AllowHexSpecifier);
}
return errorCode;
}

Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: