0
votes

Hi i try to write a little application for sharepoint 2013 where we can backup our projects on an SQL Server. Now i try to loop trough 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 wich the 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 exeption.

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.

1
Could you please elaborate more on what is "SPpowerPlantList" in your code and what should be exact output do you want to return from loadProjectFromSharePoint function.Vaibhav

1 Answers

0
votes

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;

    }