RE: The CORRECT way to delete specific list items by field ID
Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I've opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. :) Thanks for the help. I appreciate this list more than you know. :) From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? [cid:image002.jpg@01CCF786.7FEB09D0]Ishai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.aumailto:is...@exd.com.au | www.sharepoint-tips.comhttp://www.sharepoint-tips.com/ | @ishaisagihttp://twitter.com/ishaisagi From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. :) In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Ah...FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting Operator == cannot be applied to operands of type int and string. Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It's the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. :) From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It's the bastard == that you're missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.auhttp://paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I've followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me Collection was modified; enumeration operation may not execute.. So I'm now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can't get the syntax right. I keep hitting Operator '||' cannot be applied to operands of type int and int. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list [cid:image004.png@01CCF786.7FEB09D0] Help? ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss inline: image001.jpginline: image002.jpginline: image004.png___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
Re: The CORRECT way to delete specific list items by field ID
On features that create lists, on feature activation I tend to check of list already exists and create it if it doesn't, and on deactivation delete the list if it's empty. That way leaving user data available until the user takes the decision to remove it Sent from my iPhone On Mar 1, 2012, at 8:37 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I’ve opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. J Thanks for the help. I appreciate this list more than you know. J From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? image002.jpgIshai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.au | www.sharepoint-tips.com | @ishaisagi From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. J In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Ah…FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting “Operator == cannot be applied to operands of type int and string.” Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It’s the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. J From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It’s the bastard == that you’re missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I’ve followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me “Collection was modified; enumeration operation may not execute.”. So I’m now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can’t get the syntax right. I keep hitting “Operator '||' cannot be applied to operands of type int and int”. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list image004.png Help? ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss ___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: The CORRECT way to delete specific list items by field ID
Hi Nigel, That sounds like good advice. In my experience, I’ve noticed that if the list already exists it doesn’t get overwritten and my sample items don’t get re-added (this is when provisioning through code alone). XML list instances are another story. :| Is this by design?? From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Nigel Witherdin Sent: Thursday, 1 March 2012 8:45 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID On features that create lists, on feature activation I tend to check of list already exists and create it if it doesn't, and on deactivation delete the list if it's empty. That way leaving user data available until the user takes the decision to remove it Sent from my iPhone On Mar 1, 2012, at 8:37 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I’ve opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. ☺ Thanks for the help. I appreciate this list more than you know. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? image002.jpgIshai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.aumailto:is...@exd.com.au | www.sharepoint-tips.comhttp://www.sharepoint-tips.com/ | @ishaisagihttp://twitter.com/ishaisagi From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. ☺ In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Ah…FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting “Operator == cannot be applied to operands of type int and string.” Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It’s the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It’s the bastard == that you’re missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.auhttp://paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I’ve followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me “Collection was modified; enumeration operation may not execute.”. So I’m now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can’t get the syntax right. I keep hitting “Operator '||' cannot be applied to operands of type int and int”. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list image004.png Help? ___ ozmoss mailing list ozmoss
RE: The CORRECT way to delete specific list items by field ID
Actually, scratch that. I perform a check first. ☺ From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 1 March 2012 9:06 AM To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Hi Nigel, That sounds like good advice. In my experience, I’ve noticed that if the list already exists it doesn’t get overwritten and my sample items don’t get re-added (this is when provisioning through code alone). XML list instances are another story. :| Is this by design?? From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Nigel Witherdin Sent: Thursday, 1 March 2012 8:45 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID On features that create lists, on feature activation I tend to check of list already exists and create it if it doesn't, and on deactivation delete the list if it's empty. That way leaving user data available until the user takes the decision to remove it Sent from my iPhone On Mar 1, 2012, at 8:37 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I’ve opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. ☺ Thanks for the help. I appreciate this list more than you know. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? image002.jpgIshai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.aumailto:is...@exd.com.au | www.sharepoint-tips.comhttp://www.sharepoint-tips.com/ | @ishaisagihttp://twitter.com/ishaisagi From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. ☺ In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Ah…FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting “Operator == cannot be applied to operands of type int and string.” Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It’s the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It’s the bastard == that you’re missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.auhttp://paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I’ve followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me “Collection was modified; enumeration operation may not execute.”. So I’m now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can’t get the syntax right. I keep hitting “Operator '||' cannot be applied to operands of type int and int”. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i
RE: The CORRECT way to delete specific list items by field ID
Can I ask opinions on the best method to check for al list’s existence in 2010? I’ve been using the TryGetList method and passing in the DisplayName but am surprised there doesn’t seem to be a way to get list by InternalName, only StaticName?? // Check if list exists try { SPList theList = SPContext.Current.Web.Lists.TryGetList(Title); if (theList!=null) { From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 1 March 2012 9:08 AM To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Actually, scratch that. I perform a check first. ☺ From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 1 March 2012 9:06 AM To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Hi Nigel, That sounds like good advice. In my experience, I’ve noticed that if the list already exists it doesn’t get overwritten and my sample items don’t get re-added (this is when provisioning through code alone). XML list instances are another story. :| Is this by design?? From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Nigel Witherdin Sent: Thursday, 1 March 2012 8:45 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID On features that create lists, on feature activation I tend to check of list already exists and create it if it doesn't, and on deactivation delete the list if it's empty. That way leaving user data available until the user takes the decision to remove it Sent from my iPhone On Mar 1, 2012, at 8:37 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I’ve opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. ☺ Thanks for the help. I appreciate this list more than you know. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? image002.jpgIshai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.aumailto:is...@exd.com.au | www.sharepoint-tips.comhttp://www.sharepoint-tips.com/ | @ishaisagihttp://twitter.com/ishaisagi From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. ☺ In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Ah…FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting “Operator == cannot be applied to operands of type int and string.” Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It’s the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. ☺ From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It’s the bastard == that you’re missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.auhttp://paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right
RE: The CORRECT way to delete specific list items by field ID
Yep I use TryGetList, and I have a common function to delete list if empty which I call on feature deactivate: /// summary /// Delete list if exists on given web and has no items /// /summary /// param name=webSPWeb list exists on/param /// param name=listTitleTitle of list to delete/param public static void DeleteListIfEmpty(SPWeb web, string listTitle) { SPList list = web.Lists.TryGetList(listTitle); if (list != null list.ItemCount == 0) { list.Delete(); } } From: paul.no...@ceosyd.catholic.edu.au To: ozmoss@ozmoss.com Date: Thu, 1 Mar 2012 09:40:14 +1100 Subject: RE: The CORRECT way to delete specific list items by field ID Can I ask opinions on the best method to check for al list’s existence in 2010? I’ve been using the TryGetList method and passing in the DisplayName but am surprised there doesn’t seem to be a way to get list by InternalName, only StaticName?? // Check if list exists try { SPList theList = SPContext.Current.Web.Lists.TryGetList(Title); if (theList!=null) { From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 1 March 2012 9:08 AM To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Actually, scratch that. I perform a check first. J From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Thursday, 1 March 2012 9:06 AM To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Hi Nigel, That sounds like good advice. In my experience, I’ve noticed that if the list already exists it doesn’t get overwritten and my sample items don’t get re-added (this is when provisioning through code alone). XML list instances are another story. K Is this by design?? From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Nigel Witherdin Sent: Thursday, 1 March 2012 8:45 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID On features that create lists, on feature activation I tend to check of list already exists and create it if it doesn't, and on deactivation delete the list if it's empty. That way leaving user data available until the user takes the decision to remove it Sent from my iPhone On Mar 1, 2012, at 8:37 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Yes, I was aware of that method and had used it. But then I realised the IDs increment regardless of whether items have been deleted. So I’ve opted to just send all the lists to the Recycle Bin on FeatureDeactivated. Much cleaner. J Thanks for the help. I appreciate this list more than you know. J From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Ishai Sagi Sent: Thursday, 1 March 2012 6:02 AM To: ozMOSS Subject: RE: The CORRECT way to delete specific list items by field ID Also - list.Items.DeleteItemById might be a better way? image002.jpgIshai Sagi | Solutions Architect 0488 789 786 | is...@exd.com.au | www.sharepoint-tips.com | @ishaisagi From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Paul Noone Sent: Tuesday, 28 February 2012 18:36 To: 'ozMOSS' Subject: RE: The CORRECT way to delete specific list items by field ID Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. J In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Ah…FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting “Operator == cannot be applied to operands of type int and string.” Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It’s the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. J From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun
Re: The CORRECT way to delete specific list items by field ID
Hi Paul, It's the bastard == that you're missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I've followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me Collection was modified; enumeration operation may not execute.. So I'm now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can't get the syntax right. I keep hitting Operator '||' cannot be applied to operands of type int and int. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list [cid:3413355671_117753] Help? inline: image.png___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: The CORRECT way to delete specific list items by field ID
Ah...FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting Operator == cannot be applied to operands of type int and string. Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It's the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. :) From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It's the bastard == that you're missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I've followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me Collection was modified; enumeration operation may not execute.. So I'm now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can't get the syntax right. I keep hitting Operator '||' cannot be applied to operands of type int and int. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list [cid:image001.png@01CCF6D7.2AF03A90] Help? inline: image001.png___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss
RE: The CORRECT way to delete specific list items by field ID
Thanks Sez. I had learnt that lesson with a previous custom action to bulk delete items. :) In this case I do want them gone for good when the feature is deactivated. They are just dummy items. From: ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.com] On Behalf Of Sezai Komur Sent: Wednesday, 29 February 2012 12:41 PM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Another consideration when using SPListItem.Delete() in a custom solution is... do you want the deleted item completely or do you want it to go to the recycle bin? .Delete() bypasses the recycle bin completely and permanently deletes the item, if you want users to be able to recycle the deleted items instead use the .Recycle() method. http://blog.mastykarz.nl/programmatically-deleting-list-items-documents-recycle-bin/ http://msdn.microsoft.com/en-us/microsoft.sharepoint.splistitem.recycle Sezai. On Wed, Feb 29, 2012 at 8:41 AM, Paul Noone paul.no...@ceosyd.catholic.edu.aumailto:paul.no...@ceosyd.catholic.edu.au wrote: Ah...FFS. Thanks Mark. I had that initially but the IDs were in quotes and I was getting Operator == cannot be applied to operands of type int and string. Which threw me completely! The following simplified method allso seems to work. // Delete sample items SPListItem item1 = bugsList.GetItemById(1); item1.Delete(); SPListItem item2 = bugsList.GetItemById(2); item2.Delete(); It's the indexder and the SPItemCollection that seem to cause the issues. My first attempt was to remove the first two items by index which fails because index(0) changes with each deletion. :) From: ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com [mailto:ozmoss-boun...@ozmoss.commailto:ozmoss-boun...@ozmoss.com] On Behalf Of Mark Daunt Sent: Wednesday, 29 February 2012 11:21 AM To: ozMOSS Subject: Re: The CORRECT way to delete specific list items by field ID Hi Paul, It's the bastard == that you're missing in your if statement :-) Mark On 29/02/12 10:18 AM, Paul Noone paul.no...@ceosyd.catholic.edu.auhttp://paul.no...@ceosyd.catholic.edu.au wrote: Hi all, This should be easy, right? I've followed several methods as defined in MSDN documentation but nothing worked as expected. A froeach loop was giving me Collection was modified; enumeration operation may not execute.. So I'm now using a decrementing for loop and getting the items by title. While this works, it is not ideal. I want to get the items by their field ID value but can't get the syntax right. I keep hitting Operator '||' cannot be applied to operands of type int and int. try{ SPList bugsList = oWeb.Lists[Project Issues Register]; for (int i = bugsList.Items.Count - 1; i = 0; i--) { if (bugsList.Items[i].ID = 1 || bugsList.Items[i].ID = 2) { bugsList.Items.Delete(i); } } } Figure 1 - Sample list [cid:image001.png@01CCF6E7.33996170] Help? ___ ozmoss mailing list ozmoss@ozmoss.commailto:ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss inline: image001.png___ ozmoss mailing list ozmoss@ozmoss.com http://prdlxvm0001.codify.net/mailman/listinfo/ozmoss