Пример #1
0
        public async Task <bool> AddItemToList(string listName, SharepointListItem item)
        {
            try
            {
                // Authentication.
                using (var context = GetSharepointSiteContext())
                {
                    Microsoft.SharePoint.Client.List eventList = context.Web.Lists.GetByTitle(listName);
                    ListItemCreationInformation      itemInfo  = new ListItemCreationInformation();

                    ListItem listItemToAdd = eventList.AddItem(itemInfo);
                    foreach (var field in item)
                    {
                        listItemToAdd[field.Key] = field.Value;
                    }
                    listItemToAdd.Update();
                    await context.ExecuteQueryAsync();

                    return(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected error occurred while adding an item.");
                Console.WriteLine(e.Message);
                return(false);
            }
        }
Пример #2
0
        public async Task <bool> AddItemToList(string listName, SharepointListItem item)
        {
            try
            {
                // Authentication.
                using (var context = _authenticationManager.GetContext(_sharepointUrl))
                {
                    Microsoft.SharePoint.Client.List eventList = context.Web.Lists.GetByTitle(listName);
                    ListItemCreationInformation      itemInfo  = new ListItemCreationInformation();

                    ListItem listItemToAdd = eventList.AddItem(itemInfo);
                    foreach (var field in item)
                    {
                        listItemToAdd[field.Key] = field.Value;
                    }
                    listItemToAdd.Update();
                    await context.ExecuteQueryAsync();

                    return(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("An unexpected error occurred while adding an item.");
                Console.WriteLine(e.Message);
                return(false);
            }
            // When add item to list, the target list may not exist.
            // Then it should be created and set privilege.
            // Should modify code here and with the help of create list function.
        }