Search code examples
c#sharepointsharepoint-2013

SharePoint get content from list


I tried to write a little application for SharePoint 2013 where we can backup our projects on an SQL Server. Now I try to loop through all projects on SharePoint so I can get the content of the fields. Like country = austria.

I tried to follow this guide but had no luck: https://msdn.microsoft.com/en-us/library/office/fp179912.aspx

Here is that what I got:

//Loads only a Projeclist from sharepoint
public SPpowerPlantList loadProjectFromSharePoint()
{
    SPpowerPlantList pplist = new SPpowerPlantList();
    ClientContext context = new ClientContext(powerPlantSite);
    Web web = context.Web;
    context.Load(web.Lists);
    context.ExecuteQuery();
    foreach (List list in web.Lists)
    {
        SPpowerPlant pp = new SPpowerPlant();
        //Stuff like this one should work but dont....
        pp.country = list.country
    } 
    return pplist;
}

Any advice would be great and sorry for my English

EDIT: SPpowerPlantList should be a List of all Projects of the Project-List from SharePoint. And the loadProjectsFromSharepoint is supposed to get a list of Projects which then I can start to add the values to the sql Server. Stuff like SQL Table value = Sharepoint Field Value.

EDIT2 So the access to the files now work for a few fields but know I get an "The property or field has not been initialized. It has not been requested or the request has not been executed. It may need to be explicitly requested exception."

Here is the new code: (some fields work like currency)

//Loads only a Projeclist from sharepoint
    public SPpowerPlantList loadProjectFromSharePoint()
    {

        SPpowerPlantList powerPlantList = new SPpowerPlantList();
        
        ClientContext context = new ClientContext(powerPlantSite);
      
        List powerPlantsList = context.Web.Lists.GetByTitle("Power Plants");

        CamlQuery query = CamlQuery.CreateAllItemsQuery();
        query.ViewXml = @"<View><Query> </Query></View>";
        ListItemCollection items = powerPlantsList.GetItems(query);

        context.Load(items);
        context.ExecuteQuery();
        foreach (ListItem listItem in items)
        {
            SPpowerPlant powerPlant = new SPpowerPlant();

            powerPlant.projectName = listItem["Project"].ToString();
            powerPlant.location = listItem["Loacation"].ToString();
            powerPlant.country = listItem["Country"].ToString();
            powerPlant.currency = listItem["Currency"].ToString();
            powerPlant.shortName = listItem["Short Name"].ToString();
            powerPlant.spaceUrl = listItem["Space"].ToString();
            powerPlant.numberOfWtgs = Convert.ToInt32(listItem["Number of WTGs"]);
            powerPlant.mwWtg = Convert.ToDouble(listItem["MW WTG"]);
            powerPlant.mwTotal = Convert.ToDouble(listItem["MW Total"]);
            powerPlant.projectShareWeb = Convert.ToDouble(listItem["Project Share "]);
            powerPlant.mwWeb = Convert.ToDouble(listItem["MW "]);
            powerPlant.phaseDescription = listItem["Phase Description"].ToString();
            powerPlant.projectProgress = Convert.ToDouble(listItem["Project Progress"]);
            powerPlant.mwDeveloped = Convert.ToDouble(listItem["MW developed"]);
            powerPlant.possibleWtgTypes = listItem["Possible WTG Types"].ToString();
            powerPlant.hubHeight = listItem["Hub Height"].ToString();
            powerPlant.allPermits = Convert.ToDateTime(listItem["All Permits"]);
            powerPlant.cod = Convert.ToDateTime(listItem["COD"]);
            powerPlant.projectManager = listItem["Project manager"].ToString();
            powerPlant.technology = listItem["Technology"].ToString();
            powerPlant.state = listItem["State"].ToString();
            powerPlant.stateSince = Convert.ToDateTime(listItem["State since"]);
            powerPlant.visibility = listItem["Visibillity"].ToString();
            powerPlant.phase = listItem["Phase"].ToString();
            powerPlant.phaseNumber = listItem["Phase Number"].ToString();
            
            //Console.WriteLine(listItem["Currency"]);

            powerPlantList.Add(powerPlant);

        }

        return powerPlantList;

    }

I tied it with an lambda expression but no success.


Solution

  • Well the problem was that my listitem["names"] where not correct. To get that stuff to work you need to go to the sharepoint site and look at the link when you sort it there you see the right name for the listitem.

    New and working form:

     //Loads only a Projeclist from sharepoint
        public SPpowerPlantList loadProjectFromSharePoint()
        {
    
            SPpowerPlantList powerPlantList = new SPpowerPlantList();
    
            ClientContext context = new ClientContext(powerPlantSite);
    
            List powerPlantsList = context.Web.Lists.GetByTitle("Power Plants");
    
            CamlQuery query = CamlQuery.CreateAllItemsQuery(100);
            //query.ViewXml = @"<View><Query> </Query></View>";
            ListItemCollection items = powerPlantsList.GetItems(query);
    
            //context.Load(web.Lists,
            //             lists => lists.Include(list => list.Title, // For each list, retrieve Title and Id. 
            //                                    list => list.Id,
            //                                    list => list.Description));
    
            context.Load(items);
            context.ExecuteQuery();
            foreach (ListItem listItem in items)
            {
                SPpowerPlant powerPlant = new SPpowerPlant();
    
                powerPlant.projectName = listItem["Title"].ToString();
                powerPlant.location = listItem["Location"].ToString();
                powerPlant.country = listItem["Country"].ToString();
                powerPlant.currency = listItem["Currency"].ToString();
                powerPlant.shortName = listItem["Short_x0020_Name"].ToString();
                powerPlant.spaceUrl = listItem["Space"].ToString();
                powerPlant.numberOfWtgs = Convert.ToInt32(listItem["Number_x0020_of_x0020_WTGs"]);
    
                //Console.WriteLine(listItem[""]);
    
                //powerPlantList.Add(powerPlant);
    
            }
    
            return null;
    
        }