Can I loop a list in SharePoint with Visual Studio?
(Using SharePoint 2010)
Hi guys, I'll try to give enough background for this to make sense.
Basically we have a list of customers with their phone numbers and other info. This list is sorted by an overdue date. We have telemarketer staff that call the customers in this list. So far we've had only one telemarketer calling the customers in the list which presents no problems. Now however we want to have two tm's calling customers in the list, and possibly more in the future. Of course the problem is we don't want them calling the same customers at the same time.
We can solve this by assigning tm's to each customer in the list, then they only see the customers they are assigned to. But since we want to call them in a certain order we would want to assign the tm's in order as well, for example:
Assume we have 3 tm's calling. The list would ideally look as follows (TM1, 2, and 3 are the tm's that would be assigned to that customer):
- Cust#1 - TM1
- Cust#2 - TM2
- Cust#3 - TM3
- Cust#4 - TM1
- Cust#5 - TM2
- Cust#6 - TM3
- Cust#7 - TM1
- Cust#8 - TM2 ... and so on.
Now the trick is that some days we may have 1 tm calling, or 3 tm's calling. So each morning before they begin the tm admin would have to assign the calls to different tm's. This get's tricky when there's 1000+ cusomters in the list. Sure we could switch the list to data view and copy and paste values in from an Excel sheet to make the assignments. Or we could open it in Access and run a script against it. But is there a way I could progra开发者_运维技巧m a workflow (or something) in Visual Studio to loop through the list for me and do this? I would need to provide it a number representing how many tm's would be calling and then it would loop through the list of customers and assign the tm's in the fashion designated above.
If you can think of a better way to do this I'm open to other ideas too. Thanks in advance!
You can absolutely loop through the items in a list. Here's an example of one way you could do it:
string[] tms = new string[] { "TM1", "TM2", "TM3" };
SPList list = SPContext.Current.Web.Lists["My List"];
SPListItemCollection items = list.Items;
for (int i = 0; i < items.Count; i++)
{
SPListItem item = items[i];
item["AssignedTM"] = tms[i % tms.Length];
item.Update();
}
The only question that remains is how to execute this code. You could put this in a console application and run it on the server (not recommended), or you could put it in a webpart, stick it on a page, grant the admin access to the page (perhaps with an accompanying form), and have (him/her) execute it daily (or when the data changes).
精彩评论