Embed C# code inside Powershell to extract List Data

Apr 19 2016 9:45 AM
Hi,
 
I am trying to extract list data using C# code inside powershell. But when i tried doing so, i am facing errors below is the code
 
$Assem = (
“Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c” ,
“Microsoft.SharePoint.Publishing, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”
)
$Source=@"
namespace codeagainstLists
{
using Microsoft.SharePoint;
using System; 
class Program
{
using (SPSite siteObj = new SPSite("<servername"))
{
using(SPWeb webObj=siteObj.OpenWeb())
{
SPList listObj = webObj.Lists["EmployeeDetails"];
foreach(SPListItem itemObj in listObj.Items)
{
Console.WriteLine(itemObj.ID);
}
}
}
}
}
 "@
Add-Type -ReferencedAssemblies $Assem -TypeDefinition $Source -Language CSharp
@object=New-Object [codeagainstLists.Program]
 
Please correct the above code and help with the functionality of List Data.
 
Thanks and Regards,
Sivapratap 
 
 
 
 

Answers (2)