Friday, July 8, 2011

Remove all survey response from web service

Well I ran into a trouble when I needed to remove all responses from survey. It is far easy to remove all items from the list when you have the permission to access the UI. However when it even comes to the survey responses, there is a big problem. There is no way to edit response in data sheet. Only way is to go to the content and structure in site settings, locate the survey list and then increase the limit to 1000 items and keep deleting in the bunch of 1000 items.

Again the problem is what if you have got more than 50,000 responses. You will end up spending hours removing 1000 items 50 times. This is definitely something which is not worth doing.

Read following posts and you will also come to know about some other stuff.

Delete List Item using web service


Remove all response from SharePoint survey

You can treat this post as continuation of above links.

In this post, I am going to show you a way to remove all responses from survey with the help of web service. Yes web service has come to a rescue as far as removing all responses from survey is concern.

I am writing one function and on button click I am calling that function. Take a list reference from the SharePoint URL interest of you and then start wiring up below code.


public static ArrayList GetListIDs(String ListName)
{
Lists.Lists ListReference = new Lists.Lists();
ListReference.Credentials = System.Net.CredentialCache.DefaultCredentials;
ListReference.Url =
"web site url/_vti_bin/Lists.asmx";

XmlDocument xmlDoc = new System.Xml.XmlDocument();

XmlNode ndViewFields =
xmlDoc.CreateNode(XmlNodeType.Element, "ViewFields", "");
ndViewFields.InnerXml = "<FieldRef Name='ID' />";

XmlNode ndListItems =
ListReference.GetListItems(ListName, null, null,
ndViewFields, "20000", null, null);

//convert String to XMLReader
XmlReaderSettings readersettings = new XmlReaderSettings();
readersettings.ConformanceLevel = ConformanceLevel.Fragment;
readersettings.IgnoreWhitespace = true;
readersettings.IgnoreComments = true;
XmlReader xmlreader =
XmlReader.Create(new StringReader(ndListItems.OuterXml), readersettings);

ArrayList lstID = new ArrayList();

while (xmlreader.Read())
{
if (xmlreader.Name == "z:row")
lstID.Add(xmlreader.GetAttribute("ows_ID").ToString());
}

return lstID;
}




Above function is used to get All IDs of items that are there in the survey. Remember 20000 is the item limit, increase this number as many items as you would like to return.


public static void DeleteItems(String ListName, ArrayList lstID)
{
try
{
Lists.Lists ListReference = new Lists.Lists();
ListReference.Credentials = System.Net.CredentialCache.DefaultCredentials;
ListReference.Timeout = 300000;
ListReference.Url =
" web site url/_vti_bin/Lists.asmx ";


string strBatch = "";

foreach (String ID in lstID)
{
strBatch += "<Method ID='1' Cmd='Delete'><Field Name='ID'>" + ID + "</Field></Method>";
}

XmlDocument xmlDoc = new System.Xml.XmlDocument();
XmlElement elBatch = xmlDoc.CreateElement("Batch");

elBatch.InnerXml = strBatch;

ListReference.UpdateListItems(ListName, elBatch);
}
catch (Exception ex)
{
throw ex;
}
}




And above code takes those IDs as a reference and deletes them from the list. The above code may not be the perfect way to do this job but it certainly better than one approach which is looping through 1 to 50000 items and delete one by one. Rather mentioned approach is better to construct a string and then pass that entire string as a one single batch to perform the operation.

Just take care that you need to increase the time out property based on the number of items that you have. Otherwise you may get a server timeout exception.

Hope this helps.

2 comments:

Angelina Marshall said...

As a writer, this is wonderful information to have. Thanks for the survey.
The survey is very fantastic. Thank you for helpful news.
Web Survey

Unknown said...

The best way to Remove All Responses from SharePoint Survey is: using "Site Content and Structure Manager". Go to Site Actions > Site Content and Structure Manager'

Find the detailed example at: How to Remove All Responses from SharePoint Survey




Share your SharePoint Experiences with us...
As good as the SharePointKings is, we want to make it even better. One of our most valuable sources of input for our Blog Posts comes from ever enthusiastic Visitors/Readers. We welcome every Visitor/Reader to contribute their experiences with SharePoint. It may be in the form of a code stub, snippet, any tips and trick or any crazy thing you have tried with SharePoint.
Send your Articles to sharepointkings@gmail.com with your Profile Summary. We will Post them. The idea is to act as a bridge between you Readers!!!

If anyone would like to have their advertisement posted on this blog, please send us the requirement details to sharepointkings@gmail.com