2016-03-02 94 views
0

嗨,我嘗試編寫一個SharePoint應用程序的小應用程序,我們可以在SQL Server上備份我們的項目。現在我試着循環遍歷SharePoint的所有項目,這樣我就可以獲得字段的內容。像國家=奧地利。Sharepoint從列表中獲取內容

我試圖按照本指南,但沒有運氣: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值= Sharepoint字段值的東西。

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表達式,但沒有成功綁。

+0

能否請您詳細闡述更多關於什麼是「SPpowerPlantList」在你的代碼應該是什麼確切的輸出,你想從loadProjectFromSharePoint函數返回。 – Vaibhav

回答

0

那麼問題是,我的listitem [「名稱」]哪裏不正確。 爲了讓這些東西起作用,您需要到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; 

    }