Convert Sharepoint list to datetable

Source: Internet
Author: User

/// <Summary>
/// Convert the Sharepoint list into a able
/// </Summary>
/// <Returns> </returns>
Private datatable listtodatatable ()
{
// Create a table Header
Datatable _ sourcedatatable;
_ Sourcedatatable = This. columnnamedatatable ();
// Splist sourcelist = psourcespweb. Lists [This. _ sourcesplistname];
// Splistitemcollection caseitems = sourcelist. items;

Spsite site = new spsite (siteurl );
Spweb web = site. openweb ();
Splist treelist = web. Lists [sourcetreelist];
Datatable dt = new datatable ();
Splistitemcollection caseitems = treelist. items;

Foreach (splistitem caseitem in caseitems)
{
Try
{
Datarow rownew = _ sourcedatatable. newrow ();
For (INT Inti = 0; Inti <this. psourcesplistfields. length; Inti ++)
{
If (caseitem [psourcesplistfields [Inti]! = NULL)
{
If (treelist. Fields [This. psourcesplistfields [Inti]. fieldvaluetype. tostring () = "Microsoft. Sharepoint. spfielduservalue ")
{
String createrusername = caseitem [This. psourcesplistfields [Inti]. tostring ();
Int Index = createrusername. indexof (';');
Int createruserid = convert. toint32 (createrusername. substring (0, index ));
Spuser createruser = web. allusers. getbyid (createruserid );
Rownew [psourcesplistfields [Inti] = createruser. Name;
}
Else
{
Object columntype = _ sourcedatatable. Columns [This. psourcesplistfields [Inti]. datatype;
Rownew [psourcesplistfields [Inti] = caseitem [psourcesplistfields [Inti];
}
}
}
_ Sourcedatatable. Rows. Add (rownew );
}
Catch
{
}
}
Return _ sourcedatatable;
}

/// <Summary>
/// Create a able field based on the Sharepoint list
/// </Summary>
/// <Returns> </returns>
Public datatable columnnamedatatable ()
{

Spsite site = new spsite (siteurl );
Spweb web = site. openweb ();
Splist treelist = web. Lists [sourcetreelist];
Datatable dt = new datatable ();
Splistitemcollection caseitems = treelist. items;

For (INT Inti = 0; Inti <psourcesplistfields. length; Inti ++)
{
Datacolumn columnnew = new datacolumn ();

Columnnew. columnname = psourcesplistfields [Inti];
DT. Columns. Add (columnnew );
}
Return DT;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.