private void t08_ListItemCount1(SPOClient spoc, bool isLibrary, string listname) { try { spoc.DeleteList(listname); } catch { }; // Create list or library with one column SPOList spol = createListOrLibrary(spoc, isLibrary, listname, new List <SPOClient.FieldSpec>() { new SPOClient.FieldSpec() { Name = "mySingleTextLine", Type = "Text" } }); string foldername = "a/b/"; var td = new[] { new { withFolder = true, root = 1, folder = 1 }, new { withFolder = false, root = 2, folder = 1 }, new { withFolder = false, root = 3, folder = 1 }, new { withFolder = true, root = 3, folder = 2 }, }; verifyFolderContent(spoc, spol, "", 0); verifyFolderContent(spoc, spol, foldername, -1); for (int i = 0; i != td.Length; i++) { addDocument(spoc, spol, (td[i].withFolder ? foldername : string.Empty) + "Document_" + i + ".pdf"); verifyFolderContent(spoc, spol, "", td[i].root); verifyFolderContent(spoc, spol, foldername, td[i].folder); } deleteListOrLibrary(spoc, listname); }
private void t05_AddDocument1(SPOTestSystem ts, bool isLibrary, string listName) { SPOClient spoc = createClient(ts) as SPOClient; spoc.Login(); // Create list or library with one column SPOList spol = createListOrLibrary(spoc, isLibrary, listName, new List <SPOClient.FieldSpec>() { new SPOClient.FieldSpec() { Name = "mySingleTextLine", Type = "Text" } }); // Test without and with subfodlers. // Always twice to handle the case where stuff already exists. addDocument(spoc, spol, "/MyFirstDocument.pdf"); addDocument(spoc, spol, "MyFirstDocument.pdf"); addDocument(spoc, spol, "sub/subsub/MyFirstDocument.pdf"); addDocument(spoc, spol, "/sub/subsub/MySecondDocument.pdf"); deleteListOrLibrary(spoc, listName); }
public SPOList CreateList(string title, bool isLibrary, List <FieldSpec> fieldSpecs = null) { ListCreationInformation lci = new ListCreationInformation() { Title = title, TemplateType = isLibrary ? (int)ListTemplateType.DocumentLibrary : (int)ListTemplateType.GenericList }; List spList = context.Web.Lists.Add(lci); context.Load(spList); spList.Update(); context.ExecuteQuery(); SPOList spol = new SPOList(spList); if (fieldSpecs != null) { foreach (FieldSpec fs in fieldSpecs) { AddFieldToList(spol, fs); } } return(spol); }
private void testOneType(SPOClient spoc, SPOList spoList, string type, string name, string value) { SPOField field = spoc.GetFields(spoList).Where(n => n.Title == name).First(); field.Value = value; List <SPOField> fields = new List <SPOField>() { field }; int itemId = spoc.AddDocument(spoList, null, null, fields); field.Value = null; spoc.GetFieldValues(spoList, itemId, fields); if (value == string.Empty) { Assert.IsNull(fields[0].Value); return; } // My theory on how things work: // Client -> Server: csom takes string and gives it to SharePoint. It is parsed by SharePoints culture // Server -> Client: csom receives data and converts it to string according to current culture Assert.AreEqual( normalize(spoc.ClientCulture, type, value), normalize(CultureInfo.CurrentCulture, type, fields[0].Value)); }
public void DeleteField(SPOList spoList, string name) { List spList = context.Web.Lists.GetById(spoList.Id); Field f = spList.Fields.GetByTitle(name); f.DeleteObject(); context.ExecuteQuery(); }
public void DeleteItem(SPOList spoList, int itemId) { List spList = context.Web.Lists.GetById(spoList.Id); ListItem li = spList.GetItemById(itemId); li.DeleteObject(); context.ExecuteQuery(); }
public int GetFolderCount(SPOList list, string foldername) { if (folders.ContainsKey(foldername)) { return(folders[foldername]); } return(-1); }
private int addDocumenToLibrary(SPOList spol, string documentPath, string filePath, List <SPOField> fields) { // Get the list List spList = context.Web.Lists.GetById(spol.Id); context.Load(spList); string documentName = documentPath.Split('/').Last(); string documentFolder = string.Join("/", documentPath.Split('/').Reverse().Skip(1).Reverse()); Folder f = spList.RootFolder; if (!string.IsNullOrEmpty(documentFolder)) { // Add the folders one by one. If folders do not exist they will be created smoothly. foreach (string folder in documentFolder.Split('/')) { f = f.Folders.Add(folder); } } // Create the document in SharePoint FileCreationInformation fci = new FileCreationInformation() { Url = documentName, Overwrite = true, Content = System.IO.File.ReadAllBytes(filePath), }; Microsoft.SharePoint.Client.File uploadedFile = f.Files.Add(fci); // Add the attribute values. Title defaults to the document name. if (fields == null) { fields = new List <SPOField>(); } foreach (SPOField spoField in fields) { string value = convert(spoField); if (value != null) { uploadedFile.ListItemAllFields[spoField.InternalName] = value; } } if (fields.Where(n => n.Title == "Title").Count() == 0) { uploadedFile.ListItemAllFields["Title"] = documentName; } // Update and execute. Return the Id of the new item. uploadedFile.ListItemAllFields.Update(); context.ExecuteQuery(); return(GetListItem(spol, documentPath)); }
/// Get all usable fields from a lists /// Basically all user defined fields are deliverd. There is the option to /// force specific fields to show up in the result. public List <SPOField> GetFields(SPOList spoList, List <string> forcedFields = null) { List <SPOField> result = new List <SPOField>(); // Ensure that the field "Title" is always in the result if (forcedFields == null) { forcedFields = new List <string>(); } if (!forcedFields.Contains("Title")) { forcedFields.Add("Title"); } // Load all field definitions from SharePoint List spList = context.Web.Lists.GetById(spoList.Id); context.Load(spList); context.Load(spList.RootFolder); context.Load(spList.Fields); context.ExecuteQuery(); // Select the field we need to deliver foreach (Field f in spList.Fields) { // Always ignore redonly fields and hidden fields // Hidden does not mean visibility in the view, by the way if (f.ReadOnlyField || f.Hidden) { continue; } // A custom defined field can be identified the SourceID attribute is a GUID XElement fl = XElement.Parse(f.SchemaXml); bool customField = (Guid.TryParse(fl.Attribute("SourceID").Value, out Guid xx)); bool forcedField = forcedFields.Contains(f.Title); if (!(forcedField || customField)) { continue; } result.Add(new SPOField() { Title = f.Title, InternalName = f.InternalName, TypeName = f.TypeAsString, DefaultValue = f.DefaultValue, CustomField = customField, }); } return(result); }
public void AddFieldToList(SPOList spoList, FieldSpec fs) { List spList = context.Web.Lists.GetById(spoList.Id); string fieldSchema = "<Field ID='" + Guid.NewGuid() + "' " + "Type='" + fs.Type + "' " + "Name='" + fs.Name + "' " + "StaticName='" + fs.Name + "' " + "DisplayName='" + fs.Name + "' />"; Field field = spList.Fields.AddFieldAsXml(fieldSchema, true, AddFieldOptions.AddToDefaultContentType); context.ExecuteQuery(); }
public int GetFolderCount(SPOList spol, string foldername) { Folder f = getFolder(spol, foldername); if (f == null) { return(-1); } context.Load(f); context.ExecuteQuery(); return(f.ItemCount); }
private void t06_ColumnTypes1(SPOTestSystem ts) { SPOClient spoc = createClient(ts) as SPOClient; spoc.Login(); SPOList spol = createListOrLibrary(spoc, false, unitTestList); var td = new[] { new { n = 00, type = "Text", value = "HelloWorld", culture = "en-US" }, new { n = 10, type = "DateTime", value = "02/15/2017", culture = "en-US" }, new { n = 11, type = "DateTime", value = "15.02.2017", culture = "de-DE" }, new { n = 12, type = "DateTime", value = "", culture = "de-DE" }, new { n = 20, type = "Note", value = "Line1\nLine2", culture = "en-US" }, new { n = 30, type = "Choice", value = "Berlin", culture = "en-US" }, new { n = 40, type = "Number", value = "12.34", culture = "en-US" }, new { n = 41, type = "Number", value = "12,34", culture = "de-DE" }, new { n = 42, type = "Number", value = "", culture = "de-DE" }, new { n = 50, type = "Currency", value = "14.22", culture = "en-US" }, new { n = 51, type = "Currency", value = "", culture = "en-US" }, new { n = 60, type = "Boolean", value = "False", culture = "en-US" }, //new { n = 7, type = "Lookup", value = "abc", }, //new { n = 8, type = "User", value = @"0#.w|vmsp2013\administrator", }, }; int doOnly = -1; for (int i = 0; i != td.Length; i++) { if (doOnly > 0 && td[i].n != doOnly) { continue; } string name = "a" + td[i].type; string type = td[i].type; spoc.AddFieldToList(spol, new SPOClient.FieldSpec() { Name = name, Type = type }); spoc.ClientCulture = new CultureInfo(td[i].culture, false); testOneType(spoc, spol, type, name, td[i].value); spoc.DeleteField(spol, name); } Assert.IsTrue(doOnly < 0, "Not all tests executed"); deleteListOrLibrary(spoc, unitTestList); }
public void GetFieldValues(SPOList spoList, int itemId, List <SPOField> fields) { List spList = context.Web.Lists.GetById(spoList.Id); ListItem li = spList.GetItemById(itemId); context.Load(li); context.ExecuteQuery(); foreach (SPOField f in fields) { object value = li.FieldValues[f.InternalName]; if (value != null) { f.Value = value.ToString(); } } }
// Add a document and set the attribute values. // There are different paths for libraries and lists public int AddDocument(SPOList spol, string documentPath, string filePath, List <SPOField> fields) { if (!String.IsNullOrEmpty(documentPath) && documentPath[0] == '/') { documentPath = documentPath.Substring(1, documentPath.Length - 1); } if (isLibrary(spol)) { return(addDocumenToLibrary(spol, documentPath, filePath, fields)); } else { return(addDocumentToList(spol, documentPath, filePath, fields)); } }
public int AddDocument(SPOList list, string documentPath, string filePath, List <SPOField> fields) { string folder = string.Join("/", documentPath.Split('/').Reverse().Skip(1).Reverse()); if (!folders.ContainsKey(folder)) { folders[folder] = 0; } folders[folder]++; LastExportResult = new ExportResult() { ListTitle = list.Title, DocumentPath = documentPath, Folder = folder, FilePath = filePath, Fields = fields, }; return(fields.Count); }
private Folder getFolder(SPOList spol, string foldername) { if (!string.IsNullOrEmpty(foldername) && foldername[foldername.Length - 1] == '/') { foldername = foldername.Substring(0, foldername.Length - 1); } List spList = context.Web.Lists.GetById(spol.Id); context.Load(spList); if (string.IsNullOrEmpty(foldername)) { return(spList.RootFolder); } else { return(navigateToFolder(spList, foldername)); } }
public List <SPOField> GetFields(SPOList spoList, List <string> forcedFields = null) { return(new List <SPOField>() { new SPOField() { Title = "Title", DefaultValue = "use", InternalName = "Id1", CustomField = false, TypeName = "Text" }, new SPOField() { Title = "Name1", DefaultValue = "not", InternalName = "Id2", CustomField = false, TypeName = "Text" }, new SPOField() { Title = "Name2", DefaultValue = "use", InternalName = "Id3", CustomField = true, TypeName = "Boolean" }, new SPOField() { Title = "Name3", DefaultValue = "not", InternalName = "Id4", CustomField = true, TypeName = "Unsupported" }, }); }
private void t04_GetFields1(SPOTestSystem ts) { ISPOClient spoClient = createClient(ts); spoClient.Login(); // Ensure there is a minimum number of field returned for our test library SPOList testLibrary = ((SPOClient)spoClient).GetListByTitle(ts.TestLibrary); List <SPOField> result = spoClient.GetFields(testLibrary); Assert.IsTrue(ts.FieldMin <= result.Count); Assert.AreEqual(1, result.Where(n => n.Title == "Title").Count()); // Ensure the forcedField parameter worlds; "Title" should not matter. int cnt = result.Count; result = spoClient.GetFields(testLibrary, new List <string>() { "Name", "Title" }); Assert.AreEqual(cnt + 1, result.Count); }
public int GetListItem(SPOList spoList, string documentPath) { List spList = context.Web.Lists.GetById(spoList.Id); context.Load(spList.RootFolder); context.ExecuteQuery(); string path = spList.RootFolder.ServerRelativeUrl + "/" + documentPath; CamlQuery query = new CamlQuery() { ViewXml = "<View Scope='RecursiveAll'>" + "<Query>" + $"<Where><Eq><FieldRef Name='FileRef'/><Value Type='File'>{path}</Value></Eq></Where>" + "</Query>" + "</View>" }; // execute the query ListItemCollection listItems = spList.GetItems(query); context.Load(listItems); context.ExecuteQuery(); return(listItems.First().Id); }
private void addDocument(SPOClient spoc, SPOList spol, string documentPath) { // fields should contain just the one custom column.. List <SPOField> fields = spoc.GetFields(spol).Where(n => n.Title != "Title").ToList(); // Set a field value and upload document foreach (SPOField f in fields) { f.Value = "Hello World!"; } int itemId = spoc.AddDocument(spol, documentPath, testDocument, fields); // Verify that the attribute is set foreach (SPOField f in fields) { f.Value = null; } spoc.GetFieldValues(spol, itemId, fields); foreach (SPOField f in fields) { Assert.AreEqual("Hello World!", f.Value); } //spoc.DeleteItem(spol, itemId); }
private bool isLibrary(SPOList spol) { return(spol.TemplateType == (int)ListTemplateType.DocumentLibrary); }
public bool FolderExists(SPOList spol, string foldername) { return(getFolder(spol, foldername) != null); }
// Lists are more complex to handle the libraries. // If the is not´document given we do not upload one. (Used for unit testing) private int addDocumentToList(SPOList spol, string documentPath, string filePath, List <SPOField> fields) { // Get the list List spList = context.Web.Lists.GetById(spol.Id); context.Load(spList); context.Load(spList.RootFolder); context.ExecuteQuery(); string serverRelativeUrl = spList.RootFolder.ServerRelativeUrl; string documentName = null; string documentFolder = null; if (documentPath != null) { documentName = documentPath.Split('/').Last(); documentFolder = string.Join("/", documentPath.Split('/').Reverse().Skip(1).Reverse()); } ListItemCreationInformation lici = new ListItemCreationInformation(); // Create subfolders as needed if (documentPath != null) { // Figure out which folders need to be created List <FolderCreationSpec> folderCreatíonSequence = calculateFolderCreationSequence(serverRelativeUrl, getAllFolderNames(spList), documentPath); // Create those folders foreach (FolderCreationSpec fcs in folderCreatíonSequence) { createFolder(spList, fcs.Path, fcs.Foldername); } // Set the FolderUrl for the item creation lici.FolderUrl = serverRelativeUrl; if (!string.IsNullOrEmpty(documentFolder)) { lici.FolderUrl += "/" + documentFolder; } } // Create the item ListItem newItem = spList.AddItem(lici); // Set attribute values. Default for "Title" is the document name. if (fields == null) { fields = new List <SPOField>(); } foreach (SPOField spoField in fields) { string value = convert(spoField); if (value != null) { newItem[spoField.InternalName] = value; } } if (fields.Where(n => n.Title == "Title").Count() == 0) { newItem["Title"] = documentName; } newItem.Update(); // Attach the file if (documentPath != null) { AttachmentCreationInformation attachment = new AttachmentCreationInformation() { FileName = documentName, ContentStream = new MemoryStream(System.IO.File.ReadAllBytes(filePath)) }; Attachment att = newItem.AttachmentFiles.Add(attachment); context.Load(att); } // Execution... context.ExecuteQuery(); return(newItem.Id); }
public void verifyFolderContent(SPOClient spoc, SPOList spol, string foldername, int folderCnt) { Assert.AreEqual(folderCnt, spoc.GetFolderCount(spol, foldername)); }
public bool FolderExists(SPOList spol, string foldername) { int folderNumber = int.Parse(foldername.Substring(foldername.Length - 4, 4)); return(folderNumber <= MaxExistingFolderNumer); }