Sharepoint从列表

时间:2016-03-02 15:56:18

标签: c# sharepoint sharepoint-2013

您好我尝试为sharepoint 2013编写一个小应用程序,我们可以在SQL Server上备份我们的项目。现在我尝试循环使用sharepoint上的所有项目,这样我就可以得到字段的内容。喜欢country = austria。

我试图遵循本指南,但没有运气:https://msdn.microsoft.com/en-us/library/office/fp179912.aspx

这就是我得到的:

//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;
}

任何建议都会很棒,对我的英语很抱歉

编辑: SPpowerPlantList应该是Sharepoint项目列表中所有项目的列表。并且loadProjectsFromSharepoint应该得到一个项目列表,我可以开始将值添加到sql Server。像SQL Table value = Sharepoint Field Value。

EDIT2 因此,对文件的访问现在适用于几个字段,但我知道我得到了一个属性或字段尚未初始化。尚未请求或请求尚未执行。它可能需要明确请求exeption。

以下是新代码:(某些字段的工作方式与货币相同)

//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;

    }

我将它与lambda表达联系起来但没有成功。

1 个答案:

答案 0 :(得分:0)

问题是我的listitem [“names”]哪里不正确。 要使这些东西工作,您需要转到sharepoint站点并查看链接,当您在那里排序时,您会看到正确的listitem名称。

新的工作形式:

 //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;

    }