Right, then in that case:
Code:if (list.items.Any()) { var numberToTake = 5 / list.items.Count() * 100; var theFivePercentOfRecords = list.items.OrderBy(each => Guid.NewGuid()).Take(numberToTake); }
var numberToTake = ((5 / (double)list.Items.Count()) * 100);
var theFivePercentOfRecords = list.Items.OrderBy(each => Guid.NewGuid()).Take((int)numberToTake);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
//Declare sites
SPSite sourceSite = new SPSite(@"http://sharepoint");
//Declare webs
SPWeb sourceWeb = sourceSite.RootWeb;
//Declare lists
SPLists sourceList = sourceWeb.GetList("/Lists/Staff Contacts");
SPListItemCollection sourceItems = sourceList.Items;
try
{
if (sourceList.Items.Any())
{
var numberToTake = 5 / sourceList.Items.Count() * 100;
var thePercentOfRecords = sourceList.Items.OrderBy(each => Guid.NewGuid()).Take(numberToTake);
}
foreach (int n in thePercentOfRecords)
{
SPListItem item = sourceList.Items[n];
String strFirstName = item["First Name"].ToString();
String strLastName = item["Last Name"].ToString();
String strFullName = strFirstName + " " + strLastName;
String strTeam = item["Team"].ToString();
CopyItem(strFullName, strTeam);
}
sourceWeb.Dispose();
sourceSite.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static void CopyItem(String name, String team)
{
//Declare sites
SPSite destSite = new SPSite(@"http://sharepoint/communications");
//Declare webs
SPWeb destWeb = destSite.OpenWeb();
//Declare lists
SPList destList = destWeb.Lists["Staff Satisfaction"];
SPListItemCollection listItems = destList.Items;
SPListItem item = listItems.Add();
item["Staff Member"] = name;
item["Team"] = team;
item.Update();
destWeb.Dispose();
destSite.Dispose();
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
//Declare sites
SPSite sourceSite = new SPSite(@"http://sharepoint");
//Declare webs
SPWeb sourceWeb = sourceSite.RootWeb;
//Declare lists
SPList sourceList = sourceWeb.GetList("/Lists/Staff Contacts");
SPListItemCollection sourceItems = sourceList.Items;
//Get a list of integers (5% of total)
var queryset = RandomGenerator(0, sourceItems.Count, 5);
foreach (int n in queryset)
{
SPListItem item = sourceList.Items[n];
String strFirstName = item["First Name"].ToString();
String strLastName = item["Last Name"].ToString();
String strFullName = strFirstName + " " + strLastName;
String strTeam = item["Team"].ToString();
CopyItem(strFullName, strTeam);
}
sourceWeb.Dispose();
sourceSite.Dispose();
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
private static void CopyItem(String name, String team)
{
//Declare sites
SPSite destSite = new SPSite(@"http://sharepoint/communications");
//Declare webs
SPWeb destWeb = destSite.OpenWeb();
//Declare lists
SPList destList = destWeb.Lists["Staff Satisfaction"];
SPListItemCollection listItems = destList.Items;
SPListItem item = listItems.Add();
item["Staff Member"] = name;
item["Team"] = team;
item.Update();
destWeb.Dispose();
destSite.Dispose();
}
private static List<int> RandomGenerator(int start, int count, int percentage)
{
//Create two new lists
List<int> list = new List<int>();
List<int> result = new List<int>();
//Populate a temporary list with all the possible values between start value + count
for (int i = start; i < count; i++)
{
list.Add(i);
}
if (list.Any())
{
//Get the number of required values
var numberToTake = ((percentage / (double)list.Count()) * 100);
//Get a subset of the total
var records = list.OrderBy(each => Guid.NewGuid()).Take((int)numberToTake);
//Add the results to a list and return
foreach (int i in records)
{
result.Add(i);
}
return result;
}
else {
return null;
}
}
}
}
From MSDN:
SharePoint 2010....
public class SPListItemCollection : SPBaseCollection, IEnumerable
WSS 3.0...
public class SPListItemCollection : SPBaseCollection
Does that not suggest that only 2010 implements IEnumerable?
I'm trying this in a VM and have added references to System.Core.dll and added using System.Linq and it doesn't work.
It implements SPBaseCollection, which implements ICollection, which extends IEnumerable.
It's not possible to use foreach on anything that doesn't implement IEnumerable. Try it. The error message will tell you exactly that.
The error message I was getting was this:
'Microsoft.SharePoint.SPListItemCollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'Microsoft.SharePoint.SPListItemCollection' could be found (are you missing a using directive or an assembly reference?)