Come attraversare la libreria di SharePoint nel modello di oggetti client?
-
09-12-2019 - |
Domanda
Ho la libreria di SharePoint e voglio attraversare tutte le cartelle e la sottomarità della cartella utilizzando il modello dell'oggetto client. Qualcuno può avere una soluzione?
Soluzione
Il seguente codice visualizzerà tutte le librerie e le cartelle di ciascuna libreria in TreeView
private void frmForm1_Load(object sender, EventArgs e)
{
using (ClientContext clientcontext= new ClientContext("http://your server"))
{
//Load Libraries from SharePoint
clientcontext.Load(clientcontext.Web.Lists);
clientcontext.ExecuteQuery();
foreach (List list in clientcontext.Web.Lists)
{
try
{
if (list.BaseType.ToString() == "DocumentLibrary" && !list.IsApplicationList && !list.Hidden && list.Title != "Form Templates" && list.Title != "Customized Reports" && list.Title != "Site Collection Documents" && list.Title != "Site Collection Images" && list.Title != "Images")
{
clientcontext.Load(list);
clientcontext.ExecuteQuery();
clientcontext.Load(list.RootFolder);
clientcontext.Load(list.RootFolder.Folders);
clientcontext.ExecuteQuery();
TreeViewLibraries.ShowLines = true;
TreeNode LibraryNode = new TreeNode(list.Title);
TreeViewLibraries.Nodes.Add(LibraryNode);
foreach (Folder SubFolder in list.RootFolder.Folders)
{
if (SubFolder.Name != "Forms")
{
TreeNode MainNode = new TreeNode(SubFolder.Name);
LibraryNode.Nodes.Add(MainNode);
FillTreeViewNodes(SubFolder, MainNode, clientcontext);
}
}
}
}
}
}
}
//Recursive Function
public void FillTreeViewNodes(Folder SubFolder, TreeNode MainNode, ClientContext clientcontext)
{
clientcontext.Load(SubFolder.Folders);
clientcontext.ExecuteQuery();
foreach (Folder Fol in SubFolder.Folders)
{
TreeNode SubNode = new TreeNode(Fol.Name);
MainNode.Nodes.Add(SubNode);
FillTreeViewNodes(Fol, SubNode, clientcontext);
}
}
.
È possibile modificare il codice secondo il tuo requisito: -)
Uscita:
Altri suggerimenti
Abbastanza accanto ... Avrei questo oggi ... qui vai ...
NetworkCredential credentials = new NetworkCredential("username", "password", "domain");
ClientContext clientContext = new ClientContext("http://sharepoint/web");
clientContext.Credentials = credentials;
CamlQuery query = new CamlQuery();
query.ViewXml = "<View Scope='Recursive' />";
ListItemCollection docs = clientContext.Web.Lists.GetByTitle("ListName").GetItems(query);
clientContext.Load(docs);
clientContext.ExecuteQuery();
.
Dopo che solo foreach attraverso i ListItems restituiti da Docs.
public void Main()
{
using (var clientContext = new ClientContext(@"https://microsoft.sharepoint.com/teams/PAF/"))
{
var passWord = new System.Security.SecureString();
foreach (char c in "NextGenReport@2014".ToCharArray()) passWord.AppendChar(c);
clientContext.Credentials = new SharePointOnlineCredentials("smsprpt@microsoft.com", passWord);
var uploadFilePath = @"G:\\RFY\\SMB\\Weekly Refresh\\SMB_RFY_V1.1 - Weekly Refresh.xlsx";
using (var fs = new FileStream(uploadFilePath, FileMode.Open))
{
var fi = new FileInfo(uploadFilePath);
var list = clientContext.Web.Lists.GetByTitle("Reports");
clientContext.Load(list.RootFolder);
clientContext.Load(list.RootFolder.Folders);
clientContext.ExecuteQuery();
Microsoft.SharePoint.Client.File.SaveBinaryDirect(clientContext, "/teams/PAF/Reports/FY15 Reporting/Partner/SMB_RFY_V1.1 - Weekly Refresh.xlsx", fs, true);
}
}
Dts.TaskResult = (int)ScriptResults.Success;
}
. Puoi farlo tramite SpQuery:
SPQuery query = new SPQuery();
//Condition to check the item type is folder or not
query.Query = “<Where><Eq><FieldRef Name=’FSObjType’/><Value Type=’Lookup’>1</Value></Eq></Where>”;
//Get all the items including subfolders from the list
query.ViewAttributes = “Scope=’RecursiveAll’”;
.
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a sharepoint.stackexchange