Programatically delete All items from a List

I had to delete and re-populate items in a SharePoint List (more than 4000 items!) using a Web Part. Initially I tried by enumerating the list items and deleting them one by one. But it was taking a long time to delete the list items and re-populate the list with information collected from various sub-sites by crawling throught them. After some googling I fould another way of deleting the List items in bulk using CAML query. The code is give below. I noticed a significant difference in time taken for deleting the list items between using deleting items one by one and deleting the items in bulk using CAML.

Code starts —–>

////Delete the existing entries from list using CAML query
System.Text.StringBuilder sbDelete = new System.Text.StringBuilder();
sbDelete.Append(“<?xml version=\”1.0\” encoding=\”UTF-8\”?><Batch>”);
string command = “<Method><SetList Scope=\”Request\”>” + list.ID + “</SetList>” +
       “<SetVar Name=\”ID\”>{0}</SetVar>” +
       “<SetVar Name=\”Cmd\”>Delete</SetVar></Method>”;

foreach (SPListItem item in list.Items)
{
 sbDelete.Append(String.Format(command, Convert.ToString(item.ID)));
}
sbDelete.Append(“</Batch>”);
web.ProcessBatchData(sbDelete.ToString());

<——- Code ends

I found this information from the blog Mwalimu’s Corner.

Advertisements

CAML queries with CAML Query Builder

CAML queries can be easily created using a tool – U2U CAML Query Builder. Using teh U2U CAML Query Builder, the SharePoint Lists or Libraries can be opened and CAML queries for various operations can be created using the user interface. U2U CAML Query Builder 2007 can be downloaded from the U2U website.

More information about how to work with U2U CAML Query Builder can be found here.

Error: Code-blocks are not allowed in this file

I have experienced this error when I inserted some server side code into a SharePoint page. The reason for this is, the web server is configured for not to allow server side code in pages. You can modify this configuration in the web configuration file as described below.

Open web.config file and put the following code block in it.

<PageParserPaths>
  <PageParserPath VirtualPath=”/pages/test.aspx” CompilationMode=”Always” AllowServerSideScript=”true” />
 </PageParserPaths>

Site Actions menu missing

Some times I have found that the Site Actions menu missing when I login even though I’m the site collection administrator. There can be many reasons for this. I found two of them in which the second one was the reason in my case.

  1. If the the issue occures after a backup/restore of the site collection, the destination server may contain a local administrator users with same name as site collection administrator.
  2. The site collection minght have became read only. Check it throuh Central Administration website. (Central Administration > Application Management > Site collection quotas and locks)

Adding Quick Launch to Custom aspx Pages

One of the best methods for organizign information at the same level in a SharePoint site is creating custom Web Part Pages. But in WSS 3.0, the main problem with custom aspx pages is that, the custom pages do not include Quick Launch. In order to include Quick Launch into custom aspx pages in WSS 3.0, perform the following steps.

  1. In SharePoint Designer, find your master page. 
  2. Find the line :
    <asp:ContentPlaceHolder id=”PlaceHolderLeftNavBar” runat=”server”>
  3. Directly beneath it add:
      </asp:ContentPlaceHolder>
  4. Find the real closing   </asp:ContentPlaceHolder>  element and remark it out like
       <!–   </asp:ContentPlaceHolder>  –>
  5. Find the line:
       <asp:ContentPlaceHolder id=”PlaceHolderNavSpacer” runat=”server”>
  6. Directly after it add:
      </asp:ContentPlaceHolder>
  7. Find the real closing tag and remark it out
      <!–   </asp:ContentPlaceHolder>  –>
  8. Save the file

Attachments in WSS 3.0 Blog Sites

WSS 3.0 Blog site does not have the capability of uploading attachments along with a blog post and my client desperately wanted to have this feature. We could implement this feature into WSS 3.0 blog site by doing some custom coding and configurations.

Step 1: Enable Attachments button on new post and edit post pages
    Open the file C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\TEMPLATE\CONTROLTEMPLATES\DefaultTemplates.ascx in a text editor. Search for the tag with ID ‘BlogNewFormToolBar’ and place the following code in the <Template_Buttons> tag inside it.
 Code: <SharePoint:AttachmentButton runat=”server”/>
    Search for the tag with ID ‘BlogEditFormToolBar’ in the above file and placed the following code in the <Template_Buttons> tag inside it.
 Code: <SharePoint:AttachmentButton runat=”server”/>

Step 2: Develop a custom web part for listing the attachments in the Blog page (post.aspx)
Take the post list item id from the query string and find attachment collection for the item. Iterate through each attachment and list them with link to the actual attachment. The custom developed web part is deployed into the server and addedd into the post.aspx page.

Step 3: Develop a custom web part for replacing the exisiting web part for listing blogs in the main page (default.aspx)
Develop a web part for replacing the existing web part in the default.aspx page of the blog site. In the footer section add a link Attachments ([number of attachmets]). Deploy this web part into the server and add it into the default.aspx page replacing the existing web part.