Monday, 11 August 2014

Bulk deleting items from a SharePoint list

Bulk delete with SharePoint Client Object Model
We can delete bulk items from sharepoint list using data sheet view.
But if you want to do it using Client Object modal- Javascript, then use below code.

<%@ Page Language="C#" MasterPageFile="~masterurl/default.master" Inherits="Microsoft.SharePoint.WebPartPages.WebPartPage, Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<asp:content contentplaceholderid="PlaceHolderAdditionalPageHead" runat="server">
<SharePoint:ScriptLink Name="sp.js" runat="server" OnDemand="true" LoadAfterUI="true" Localizable="false" />
<script src="http://code.jquery.com/jquery.js" type="text/javascript"></script>
<script type="text/javascript" src="/_layouts/15/sp.runtime.js"></script>
<script type="text/javascript" src="/_layouts/15/sp.js"></script>
<script type="text/javascript">
$(function () {
$("#btnFindID").click(function () {
var bulkDeleteIDs = [];
//Below code will work, if check box title is ID..mean your ID column data will be available as a Title property of checkbox.
$.each($(".ms-listviewtable").find("tr"),
function (i, e0) {
$.each($(this).find(".s4-itm-cbx"),
function (k, e1) {
if (e1.checked == true) {
console.log(e1.title);
//You will get all selected Items ID
bulkDeleteIDs.push({ "ID": e1.title });
}
});
});
//If check box title is not having ID, then you have to read ID value from ID column
//Read ID based on Check box- Start
$.each($(".listViews").find("div"),
function (i, e0) {
if ($(this).css('display') == "inline-block") { //If you have multiple divs with .listViews class, then you can addition check based on style.
$.each($(this).find("tr.ms-itmhover"), function (i, opt1) {
$(this).find('td:nth-child(1)').each(function () {
var chk = $(this).find('input[type=checkbox]')[0];
if (chk.checked == true) {
$(opt1).find('td:nth-child(2)').each(function () {
console.log($(this).text());
bulkDeleteIDs.push({ "ID": $(this).text() });
});
}
});
});
}
});
//Read ID based on Check box-End
//Now we will call Delete function
bulkDelete(bulkDeleteIDs);
});
//Bulk Delete-Start
function bulkDelete(ids) {
var listName = "MyList";
if (itemIDs.length != 0) {
$.each(itemIDs, function (key, value) {
var objCtx = new SP.ClientContext();
var objList = objCtx.get_web().get_lists().getByTitle(listName);
var objListItem = objList.getItemById(value.ID);
objListItem.deleteObject();
objCtx.executeQueryAsync(
function () {
console.log("ID: " + value.ID + " has deleted.");
},
function (sender, args) {
console.log(sender, args, 'Error: ' + value.ID);
}
);
});
}
}
//Bulk Delete-End
});
</script>
</asp:content>
<asp:content contentplaceholderid="PlaceHolderMain" runat="server">
<div class="listViews" id="dvMyList" style="display:inline-block">
<input class="btn" id="btnFindID" value="Archive" type="button" />
<WebPartPages:XsltListViewWebPart runat="server" IsIncluded="True"
FrameType="None"
NoDefaultStyle="TRUE" ViewFlag="8" Title="Latest news"
PageType="PAGE_NORMALVIEW" ListUrl="Lists/MyList"
Default="FALSE" DisplayName="Server journal" __markuptype="vsattributemarkup"
ID="lvwbArchive"
ViewContentTypeId="0x" WebPart="true" Height="" Width="">
<XmlDefinition>
<View Name="MySubmissions"
MobileView="TRUE" Type="HTML" DisplayName="All items"
Url="/Lists/MyList/AllItems.aspx" Level="1" BaseViewID="1"
ContentTypeID="0x">
<Query> <OrderBy><FieldRef Name="ID" Ascending="FALSE"/></OrderBy>
<Where>
<Neq><FieldRef Name="isTrue"/><Value Type="Text">Yes</Value></Neq>
</Where>
</Query>
<ViewFields>
<FieldRef Name="ID"/>
<FieldRef Name="Attachments"/>
<FieldRef Name="Title"/>
<FieldRef Name="Author"/>
<FieldRef Name="Created"/>
<FieldRef Name="isTrue"/>
</ViewFields>
<Toolbar Type="Standard"/>
</View>
</XmlDefinition>
</WebPartPages:XsltListViewWebPart>
</div>
</asp:content>
view raw gistfile1.html hosted with ❤ by GitHub