John
John

Reputation: 4807

Batch Element in Sharepoint to Delete a List item when you do not know the ID

I want to delete an item in a list when it matches some critera using UpdateListItems web service. I dont know the ID of the list item that I want to delete but do know the criteria.

For example in SQL I could do:

DELETE FROM listName WHERE LastName='Bauer' AND FirstName='Jack'

How would you write a Batch Element to do this?

Update

Would it be something like this?

<Batch PreCalc='TRUE' OnError='Continue'>
  <Method ID='1' Cmd='Delete'>
    <Field Name='LastName'>Bauer</Field>
    <Field Name='FirstName'>Jack</Field>
  </Method>
</Batch>

Is the ID for the Method or the ID of the thing you wish to delete?

Update

I have tried the following code and the error that is returned is

Invalid URL Parameter

The URL provided contains an invalid Command or Value. Please check the URL again.

My guessing is that it is not possible to do without the ID...

Upvotes: 1

Views: 6766

Answers (4)

r.k.
r.k.

Reputation: 11

Similarly, to get list item returned from the the Sharepoint 2010 object model that can be used in C# corresponding value of a key node in xml we have to get rid of that additional character attached at position 1 in the xml. This can be done as follows:

// Code to decipher XML string returned from SharePoint Server 2010 object model list item. - Courtesy - Rajiv Kapoor - ideals.co.in. View http://www.ideals.co.in/estore/code.php for complete solution.

        string sRetVal = "";
        string myXMLStr = "​<robot><Key>ConstSigned</Key><Value>Rad Is Signed</Value><Key>CodeAddChangeReqSub</Key><Value>ACRS</Value><Key>CodeAddChangeReqInit</Key><Value>ACRI</Value><Key>CodeSupTaskComp</Key><Value>STC</Value><Key>CodeSupApprComp</Key><Value>SAC</Value><Key>CodeSupRejComp</Key><Value>SAR</Value><Key>CodeOperAppr</Key><Value>OPA</Value><Key>CodeOperRej</Key><Value>OPR</Value><Key>Oper1TaskCreated</Key><Value>OTC</Value><Key>Oper2TaskCreated</Key><Value>QCTC</Value><Key>Oper2TaskAppr</Key><Value>QCRA</Value><Key>Oper2TaskRej</Key><Value>QCRR</Value><Key>ApprPending</Key><Value>Approval Pending</Value><Key>PendingProcessing</Key><Value>Pending Processing</Value><Key>PendingReview</Key><Value>Pending Review</Value><Key>Approved</Key><Value>Approved</Value><Key>GroupOperation</Key><Value>Operation</Value><Key>WorkFlowHistoryList</Key><Value>/Lists/Workflow History</Value><Key>ColInitDateTime</Key><Value>Initiated Date Time</Value><Key>ColWorkFlowHistoryParentInst</Key><Value>Workflow History Parent Instance</Value><Key>ColWorkflowAssId</Key><Value>Workflow Association ID</Value><Key>ColListId</Key><Value>List ID</Value><Key>ColPrimaryItemId</Key><Value>Primary Item ID</Value><Key>ColDate</Key><Value>Date Occurred</Value><Key>ColOutcome</Key><Value>Outcome</Value><Key>ColDesc</Key><Value>Description</Value><Key>ColCreated</Key><Value>Created</Value><Key>ColTaskStatus</Key><Value>TaskStatus</Value><Key>ServiceReqList</Key><Value>Service Request</Value><Key>CAUEventList</Key><Value>Events</Value><Key>ReqStatus1</Key><Value>Request Status</Value><Key>ReqStatCode</Key><Value>Request Status Code</Value><Key>ColumnCode</Key><Value>Code</Value><Key>SuperUser</Key><Value>SuperUser</Value><Key>AssignedTo</Key><Value>AssignedTo</Value><Key>TaskListId</Key><Value>ID</Value><Key>DocLibId</Key><Value>List</Value><Key>WorkflowInstanceId</Key><Value>WorkflowInstanceID</Value><Key>TaskTitle</Key><Value>Title</Value><Key>Priority</Key><Value>Priority</Value><Key>ReqStatusCode</Key><Value>Request Status Code</Value><Key>ServiceRequestList</Key><Value>Service Request</Value><Key>CAUEventList</Key><Value>Events</Value><Key>ColumnDesc</Key><Value>Description</Value><Key>ColumnStatus</Key><Value>Status</Value><Key>Completed</Key><Value>Completed</Value><Key>ReqStatus</Key><Value>Request Status</Value><Key>ColumnTaskStatus</Key><Value>TaskStatus</Value><Key>ColumnPerComp</Key><Value>PercentComplete</Value><Key>Rejected</Key><Value>Rejected</Value><Key>CodeSupTaskCreated</Key><Value>STC</Value><Key>CodeSupTaskInit</Key><Value>SAI</Value><Key>Oper1TaskInit</Key><Value>OPI</Value><Key>Oper2TaskInit</Key><Value>QCRI</Value><Key>ColActedBy</Key><Value>Acted By</Value><Key>WorkFlowCode</Key><Value>Workflow Code</Value><Key>ProcCode</Key><Value>Process Code</Value></robot>";
        string myXMLStr1 = myXMLStr;
        int ln = myXMLStr.Length;
        int lnMinus1 = ln - 1;
        char[] myst = { ' ', ' ', ' '};// create a long array enough to fit the xml
        string mys = "";
        object m1 ;
        string m2 = ""; 

        myXMLStr.CopyTo(0, myst, 0, ln);            m1 = myst.Clone();
        m2 = new string(myst);// m1;// ToString();
        m2.TrimStart();
        m2.TrimEnd();
        m2 = m2.Substring(1);
        sRetVal = GetConfigVal(m2, "ProcCode");
        Console.WriteLine("****#*" + sRetVal + "*#******");

Upvotes: 1

John
John

Reputation: 4807

This does not look possible.

My way round this was to query the list and get the id's. Loop for the response pull out the id's then create a method for each ID to delete it.

Upvotes: 2

Paul-Jan
Paul-Jan

Reputation: 17278

As an addition to ChrisB's answer (formatting a CAML query in a comment didn't seem to work out), you'd make the query something like this:

SPQuery query = new SPQuery();
query.Query = "<Where><And>"+
  "<Eq><FieldRef Name='LastName'/><Value Type='Text'>Bauer</Value></Eq>"
  "<Eq><FieldRef Name='FirstName'/><Value Type='Text'>Jack</Value></Eq>"
  "</And></Where>";
SPListItemCollection items = list.GetItems(query);

(this is the object model specification, but it extends naturally into the webservices call)

Then you'd loop through the listitems and build up your batch.

Upvotes: 2

Chrisb
Chrisb

Reputation: 729

I am not that familiar with using the web services, but I assume there is one for searching. Using the API you would create an SPQuery and use CAML to get the list items you wanted to remove.

Upvotes: 0

Related Questions