Thursday, March 26, 2009

Delete List Items or Document Library Items Forever (Without sending to recycle bin)

Some weeks back I got a post from my co-worker to delete all the items from a list without being overloading items in recycle bin.

There is a good post in http://code.msdn.microsoft.com/SharePointListDelete which I'm keeping it for my own reference. This post is not my original content, and I'm only keeping this post for my own reference in development.

Firstly this method of deleting the items is more efficient with the performance on the execution for deleting let's say 10,000 of records. Also it sends only one request to delete all the records instead of individual request per items. Also I noted one more useful method spSite.RootWeb.ProcessBatchData(sbDelete.ToString());


public static void DeleteAllItems(string site, string list)
{
using (SPSite spSite = new SPSite(site)){
using (SPWeb spWeb = spSite.OpenWeb()){
StringBuilder sbDelete =BuildBatchDeleteCommand(spWeb.Lists[list]); spSite.RootWeb.ProcessBatchData(sbDelete.ToString()); spWeb.RecycleBin.DeleteAll();
//
Note: For me it didn't work for rootweb, i used spWeb.
ProcessBatchData(sbDelete.ToString());}}}
Now the method to call the XML request. This will build a list of all the items to get deleted.
private static StringBuilder BuildBatchDeleteCommand(SPList spList)
{

StringBuilder sbDelete = new
StringBuilder();

sbDelete.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?><Batch>");

string command = "<Method><SetList Scope=\"Request\">" + spList.ID +

"</SetList><SetVar Name=\"ID\">{0}</SetVar><SetVar Name=\"Cmd\">Delete</SetVar></Method>";
// Remember to delete the document library you have to add this <SetVar> too //<SetVar Name="owsfileref">{server-relative-url}</SetVar> The relative URl to the document to delete, where SPFile instance's ServerRelativeUrl property.
foreach (SPListItem item in spList.Items)
{
sbDelete.Append(string.Format(command, item.ID.ToString()));
}
sbDelete.Append("</Batch>");

return sbDelete;
}
Also I got variant of this method to delete purge the items similar way. This can be executed in the list object without calling the Rootweb.You have to call only the CAML query with each fields ids defined. This will also run as a single request batch command.

<Batch OnError="Continue">
<Method ID="1" Cmd="Delete">
<Field Name="ID">1</Field>
</Method>
<Method ID="2" Cmd="Delete">
<Field Name="ID">2</Field>
</Method>
……… so on
</Batch>

Similary for deleting the documents from the list , add the following line inside the Method tag.
<Field Name="FileRef">http://spdev/Library Name/Msg-4218faa2-727e.xml</Field>

The commands can also be executed from the Sharepoint Webservices UpdateListItems() method.

Wednesday, March 11, 2009

Sharepoint Image Rolling (Content) Webpart

Recently in one of the project there is a requirement of building a image rolling webpart. As there was time constraint to build a entire Visual Studio webpart, I took help of Content Editor Webpart and put down simple Javascript and HTML codes to make it working.
You need to create a picture library to store your images. Only disadvantage is that you need to hardcode the images and add some scripts code for any images you want to add for rolling in future.

1) Drag and drop a Content Editor webpart in your ASPX page.

2) Add the complete Script below:
<script language="javascript" type="text/javascript">

<!--
var image="";
var banners=0;
var bannerImage = new Array(4); // Modify the array count if new images added
bannerImage[0]= "http://mywebserver/PublishingImages/Image1.jpg";
bannerImage[1]= "http://mywebserver/PublishingImages/Image2.jpg";
bannerImage[2]= "http://mywebserver/PublishingImages/Image3.jpg";
bannerImage[3]= "http://mywebserver/PublishingImages/Image4.jpg";
// <<Insert new Image array here>>

var bannerURL = new Array(bannerImage.length);
bannerURL[0]= "http://www.google.com";
bannerURL[1]= "http://www.yahoo.com";
bannerURL[2]= "http://mywebserver/news/Pages/CCNNEAwards.aspx";
bannerURL[3]= "http://mywebserver/default.aspx";
// <<Insert New Image URL here>>

var link = bannerURL[0] ;
function cycle() {
if (++banners > bannerImage.length-1) banners=0;
document.banner1.src = bannerImage[banners];
window.setTimeout('cycle();',6000);
}

function urlswitch() {
link= bannerURL[banners];
return link;
}
//-->
</script>

<center>
<a href="#" onclick="this.href=urlswitch()">
<img width="225" border="0" src="http://mywebserver/Rotating%20Images/Image1.jpg" name="banner1"></a> </center>

3) To add new Image
To add a new image(Image5.jpg) just add a this line
//** URL of the image**
bannerImage[4]= "http://mywebserver/PublishingImages/Image5.jpg";
after
bannerImage[3]= "http://mywebserver/PublishingImages/Image4.jpg";
Also update the number of array item to 5 in the line.
var bannerImage = new Array(5);

Add a URL to the New Image
Add this line
bannerURL[4]= "http://mywebserver/department/HR/default.aspx"; // **URL to drill down to a site after the line containing “bannerURL[3] “;