// ---------- CONSTRUCTORS ---------- /// <summary> /// Creates a SharePointDocumentLibrary object from the underlying List object. /// </summary> /// <param name="list">The base List object representing this document library.</param> /// <param name="listItemCollection">A collection of ListItems that represents the items currently queried from the list.</param> /// <exception cref="System.ArgumentNullException">Thrown if a list or folder is not supplied.</exception> public SharePointDocumentLibrary(List list, ListItemCollection listItemCollection) : base(list, listItemCollection) { Folder = new SharePointFolder(list.RootFolder); SharePointClient.Load(list.RootFolder.Context, list.RootFolder); // Create the list of documents. foreach (ListItem item in listItemCollection) { documents.Add(new SharePointDocument(item)); } }
// ---------- METHODS ---------- /// <summary> /// Uploads a file to the folder. /// </summary> /// <param name="name">The name of the file to upload.</param> /// <param name="stream">The IO stream object representing the file's contents.</param> /// <param name="fileLength">The length of the file in bytes.</param> /// <param name="client">The SharePoint client that will be used to upload the file.</param> /// <param name="requiredFieldValues">Optional: A dictionary with values for fields that are required to be supplied /// when uploading the file to the folder in SharePoint.</param> /// <returns>True if the file could be uploaded, false otherwise.</returns> public bool UploadFile(string name, Stream stream, int fileLength, SharePointClient client, Dictionary <string, object> requiredFieldValues = null) { if (!string.IsNullOrWhiteSpace(name) && stream != null && fileLength > 0 && client != null) { try { Microsoft.SharePoint.Client.File.SaveBinaryDirect((ClientContext)folder.Context, Url + @"/" + name, stream, true); Microsoft.SharePoint.Client.File file = client.GetFileByUrl(Url + @"/" + name); // Add required field values if supplied. if (requiredFieldValues != null) { ListItem listItem = file.ListItemAllFields; foreach (string fieldName in requiredFieldValues.Keys) { listItem[fieldName] = requiredFieldValues[fieldName]; } listItem.Update(); } // Update the server with the changes. folder.Context.ExecuteQuery(); // The file is automatically checked out. Check it back in. file.CheckIn("", CheckinType.OverwriteCheckIn); return(true); } catch { // There was an error uploading the file. return(false); } } else { // Parameters were not supplied. return(false); } }
// ---------- METHODS ---------- /// <summary> /// Uploads a file to the folder. /// </summary> /// <param name="name">The name of the file to upload.</param> /// <param name="stream">The IO stream object representing the file's contents.</param> /// <param name="fileLength">The length of the file in bytes.</param> /// <param name="client">The SharePoint client that will be used to upload the file.</param> /// <param name="requiredFieldValues">Optional: A dictionary with values for fields that are required to be supplied /// when uploading the file to the folder in SharePoint.</param> /// <returns>True if the file could be uploaded, false otherwise.</returns> public bool UploadFile(string name, Stream stream, int fileLength, SharePointClient client, Dictionary<string, object> requiredFieldValues = null) { if (!string.IsNullOrWhiteSpace(name) && stream != null && fileLength > 0 && client != null) { try { Microsoft.SharePoint.Client.File.SaveBinaryDirect((ClientContext)folder.Context, Url + @"/" + name, stream, true); Microsoft.SharePoint.Client.File file = client.GetFileByUrl(Url + @"/" + name); // Add required field values if supplied. if (requiredFieldValues != null) { ListItem listItem = file.ListItemAllFields; foreach (string fieldName in requiredFieldValues.Keys) { listItem[fieldName] = requiredFieldValues[fieldName]; } listItem.Update(); } // Update the server with the changes. folder.Context.ExecuteQuery(); // The file is automatically checked out. Check it back in. file.CheckIn("", CheckinType.OverwriteCheckIn); return true; } catch { // There was an error uploading the file. return false; } } else { // Parameters were not supplied. return false; } }
// ---------- CONSTRUCTORS ---------- /// <summary> /// Creates a SharePointDocument object from the underlying ListItem object. /// </summary> /// <param name="listItem">The base ListItem object representing this document.</param> /// <exception cref="System.ArgumentNullException">Thrown if a list item is not supplied.</exception> public SharePointDocument(ListItem listItem) : base(listItem) { // Load various the objects and parameters needed into the context. SharePointClient.Load(listItem.Context, listItem.File); SharePointClient.Load(listItem.Context, listItem.File.ModifiedBy); }