Using a Console App to Extract a data from sharepoint list

痴心易碎 提交于 2020-01-06 14:38:27

问题


I have been searching for hours about the best way to do this and all I find is massively conflicting information and im at the point of breaking down.

I have a sharepoint 2013 site which naturally has a list on.

I wish to connect to this list and extract the information into a data table. shouldn't be that hard should it...

So options I have tried, I was originally planning on ssis to pull the information and transform it prior to its final destination. There is a great little adapter apparently for this the sharepoint list adapter... doesn't work with SSIS 13 !!! of course it doesn't that's the only version I can use.

So then I read up on a webservice called lists.asmx which when called and queried should return an xml version of the table... great!

I connect to the webservice and the one method that I need to use (GETLISTITEMS) which is in all the tutorials doesn't exist in SP2013!!!

can someone please give me an example or link me to a website which shows connection and query examples of either

A pulling the data via SSIS 13 B how to connect to the SP site and extract a tables information.

Please return my sanity, Thank you


回答1:


as a console app i assume you mean a c# console app in visual studio? Just google for SharePoint 2013 CSOM - ClientSide object model. You can find tons of info on that. It allows you to manage your SP environment (as well as read lists) from 'a distance' / client-side. You need the managed API dlls

https://msdn.microsoft.com/en-us/library/office/fp179912.aspx




回答2:


Thanks Verthosa for pointing me in the right direction, the CSOM definitely was the way to go even though it took some reading through and experimentation.

For anyone who is looking for a nice easy class to pull a list Below is my class which turns the list data into a datatable for easy calling.

You must add Using Microsoft.SharePoint.Client - Download SP2010/2013 client SDK if not there) and include the assembly extension Microsoft.SharePoint.Client + Microsoft.SharePoint.Runtime in your references.

Call Method

DataTable dt = new DataTable();
dt = ClassName.GetList("http://SharepointSite", "Name of List Table");

Fetch and Convert Class Method

 public static DataTable GetList(string site, string listname)
    {
        ClientContext ctx = new ClientContext(site);

        List lst = ctx.Web.Lists.GetByTitle(listname);

        CamlQuery cq = CamlQuery.CreateAllItemsQuery();

        ListItemCollection lic = lst.GetItems(cq);

        ctx.Load(lic);

        ctx.ExecuteQuery();
        DataTable dt = new DataTable();

        foreach (var field in lic[0].FieldValues.Keys)
        {
            dt.Columns.Add(field);
        }

        foreach (var item in lic)
        {
            DataRow dr = dt.NewRow();

            foreach (var obj in item.FieldValues)
            {
                if (obj.Value != null)
                {
                    string type = obj.Value.GetType().FullName;

                    if (type == "Microsoft.SharePoint.Client.FieldLookupValue")
                    {
                        dr[obj.Key] = ((FieldLookupValue)obj.Value).LookupValue;
                    }
                    else if (type == "Microsoft.SharePoint.Client.FieldUserValue")
                    {
                        dr[obj.Key] = ((FieldUserValue)obj.Value).LookupValue;
                    }
                    else
                    {
                        dr[obj.Key] = obj.Value;
                    }
                }
                else
                {
                    dr[obj.Key] = null;
                }
            }

            dt.Rows.Add(dr);
        }

        return dt;
    }


来源:https://stackoverflow.com/questions/36914555/using-a-console-app-to-extract-a-data-from-sharepoint-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!