Delete list items from SharePoint SPListItemCollection.

rated by 0 users
This post has 1 Reply | 0 Followers

Not Ranked
Posts 3
Points 25
Tomek Prentki Posted: Thu, Jun 22 2006 8:32 AM

Example:

If you would like to delete item from SPListItemCollection by property name, you can do this by for loop with current statement below.

 

public void DeleteLinkItems(String Name)

{

   SPList userList = GetUsersSPList ();

   SPListItemCollection listItems = userList.Items;

        

   for (int i=listItems.Count-1; i>-1; i--)

   {

      SPListItem item = listItemsIdea;

      if(item["User"].ToString() == Name)

      {

         listItems.Delete(i);

      }       

   }

}

 

private SPList GetUsersSPList()

{

   //Get the site object

   SPSite site = new PSite(“http://localserver/sites/UserList”);

 

   // Get the web object

   SPWeb userList = site.OpenWeb();

                  

   // Get User list object

   return userList.Lists["User List Item"];

}

Tomek Prentki mail : tomasz_prentki@hotmail.com
Not Ranked
Posts 1
Points 5

SPSite mySiteDR = new SPSite(myActionParentListURL);
SPWeb myWebDR = mySiteDR.OpenWeb();       

    
myWebDR.AllowUnsafeUpdates = true;
SPList spListForDeletion = myWebDR.Lists["Listname"];         

  
SPQuery myActionDeleteQuery = new SPQuery();          
strCurrentUserName = myWebDR.CurrentUser.Name.ToString();   

string myActionDeleteCAML  = "";         
myActionDeleteCAML  = "<Where><And><Eq><FieldRef Name='Author' /><Value Type='User'>";
myActionDeleteCAML  = myActionDeleteCAML + strCurrentUserName+"</Value></Eq><IsNull><FieldRef Name='Title' /></IsNull></And></Where>";
myActionDeleteQuery.Query = myActionDeleteCAML.ToString();        

   
SPListItemCollection myActionItemCollectionDR = spListForDeletion.GetItems(myActionDeleteQuery);
strMyActionDRCnt = myActionItemCollectionDR.Count;


if (strMyActionDRCnt > 0)
{


for (int dRowCnt = 0; dRowCnt < strMyActionDRCnt; dRowCnt++)
{
    SPListItem item = myActionItemCollectionDR[dRowCnt];
    myActionItemCollectionDR.Delete(dRowCnt);                  
}
}


//---------->Please refer this url too http://msdn2.microsoft.com/en-us/library/ms467435.aspx?PHPSESSID=ca9tbhkv7klmem4g3b2ru2q4d4 

Regards,

Kapiljith.R

 

  • | Post Points: 5
Page 1 of 1 (2 items) | RSS
Powered by Community Server (Commercial Edition), by Telligent Systems

Copyright© 2008 Telligent Systems Inc. All rights reserved
CommunityServer.com  •  Telligent.com