Reputation: 975
I am trying to delete an item from a list and have the following xml
<Batch PreCalc='TRUE' OnError='Continue'>
<Method ID='1' Cmd='Delete'>\
<Field Name='ID'>185</Field>
</Method>
</Batch>
This is returning the following error
0x81020030 - Invalid file name
The 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.
It looks like I need to provide the fileName rather than just using the ID. My attempts to do this have failed so far.
Update
I think the XML needs to be in the following format:
<Batch PreCalc='TRUE' OnError='Continue'>
<Method ID='1' Cmd='Delete'>
<Field Name='ID'>185</Field>
<Field Name="FileRef">http://sharepoint.mycompany.com/testsite/lib/flying spider 2009-09-03 P.jpg</Field>
</Method>
</Batch>
No error is thrown but nothing is being deleted.
Update 2
After Alex reply I removed the spaces in the url and have removed and tabs/ newlines as this "may" cause a problem:
<Batch PreCalc='TRUE' OnError='Continue'>
<Method ID='1' Cmd='Delete'>
<Field Name='ID'>185</Field>
<Field Name="FileRef">http://sharepoint.mycompany.com/testsite/lib/flying%20spider%202009-09-03 P.jpg</Field>
</Method>
</Batch>
Again no error is thrown.
Should I be using FileRef? FileLeafRef? Should I me using the file name? relative path? URL to file?
If this matters this is a Picture Libary
Upvotes: 2
Views: 10122
Reputation: 13
You need get the ID first by GetListItems, then use this ID as primary event ID to Delete:
System.Xml.XmlDocument docQuery = new System.Xml.XmlDocument();
XmlNode ndQuery = docQuery.CreateNode(XmlNodeType.Element, "Query", "");
XmlNode ndViewFields = docQuery.CreateNode(XmlNodeType.Element, "ViewFields", "");
XmlNode ndQueryOptions = docQuery.CreateNode(XmlNodeType.Element, "QueryOptions", "");
ndQueryOptions.InnerXml = "<IncludeMandatoryColumns>FALSE</IncludeMandatoryColumns><DateInUtc>FALSE</DateInUtc>" ;
ndViewFields.InnerXml = "<FieldRef Name='ID'/><FieldRef Name='UID'/><FieldRef Name='Title'/>";
ndQuery.InnerXml = "<Where><Eq><FieldRef Name='UID'/><Value Type='String'>"+leaveId+"</Value></Eq></Where>";
System.Xml.XmlNode delNodeList = wsLists.GetListItems("Calendar","", ndQuery, ndViewFields, "", ndQueryOptions, "");
Then the ID contain the node InnerXML, here is the xml sample:
<rs:data ItemCount="1" xmlns:rs="urn:schemas-microsoft-com:rowset">
<z:row ows_ID="5558"
ows_UID="{FC94B218-C68F-ED11-8E93-5CEA1D3BBB8C}"
ows_Title="XXXXXXXXXXX "
ows_MetaInfo="5558;#"
ows__ModerationStatus="0"
ows__Level="1"
ows_fAllDayEvent="0"
ows_UniqueId="5558;#{EC74D6EF-F3AD-4213-A5F0-B54EE30F86F6}"
ows_owshiddenversion="2"
ows_FSObjType="5558;#0"
ows_Created="2023-01-09 10:34:12"
ows_PermMask="0x1b03c4312ef"
ows_Modified="2023-01-09 10:34:45"
ows_FileRef="5558;#Lists/Calendar/5558_.000"
xmlns:z="#RowsetSchema" />
</rs:data>
the attribute "ows_ID" is the ID you can use for delete! Here is the MS link for GetListItems Details: https://learn.microsoft.com/en-us/previous-versions/office/developer/sharepoint-services/ms772599(v=office.12)
Upvotes: 0
Reputation: 2340
I recently came across the same problem, you need to include the relative path to the item you are deleting as well as the id as below:
<Batch OnError="Return">
<Method ID="1">
<Field Name="ID">123456</Field>
<Field Name="FileRef">sites/library/folderNameOrFileName</Field>
</Method>
</Batch>
Upvotes: 1
Reputation: 60058
This is probably because there are spaces in the filename and SharePoint can't find the item. Have you tried replacing each space with %20
?
According to the MSDN How to: Update List Items article:
Posting the UpdateListItems method silently fails if a specified item does not exist.
If this doesn't work you could try specifying the ListVersion
and ViewName
attributes on the Batch
element. Every example I've seen specifies these.
Upvotes: 1