Search Results for

    Show / Hide Table of Contents

    Adding, updating or deleting data from Microsoft 365

    Adding, updating or deleting data (e.g. a SharePoint list item or a Teams channel message) is usually needed when you write applications using the PnP Core SDK and in this article you'll get a brief overview on how to add, update or delete data.

    In the remainder of this article you'll see a lot of context use: in this case this is a PnPContext which was obtained via the PnPContextFactory as explained in the overview article and show below:

    using (var context = await pnpContextFactory.CreateAsync("SiteToWorkWith"))
    {
        // See next chapter on how to use the PnPContext for adding, updating and deleting data
    }
    

    Adding data

    Adding of data is usually done via an Add method on the collection to which you want to add data, so if you want to add an IListItem you would use one of the Add methods (e.g. AddAsync) on the IListItemCollection collection. Given that each model (List Item, File, TeamChannel,..) requires different input for doing an add you'll see different add method signatures.

    Dictionary<string, object> item = new Dictionary<string, object>()
    {
        { "Title", "Item1" },
        { "Field A", 25 }
    };
    
    // Add the item
    var addedItem = await myList.Items.AddAsync(item);
    

    Another add example is adding (= uploading) a file to a document library:

    // Get a reference to a folder
    IFolder siteAssetsFolder = await context.Web.Folders.Where(f => f.Name == "SiteAssets").FirstOrDefaultAsync();
    
    // Upload a file by adding it to the folder's files collection
    IFile addedFile = await siteAssetsFolder.Files.AddAsync("test.docx", 
                      System.IO.File.OpenRead($".{Path.DirectorySeparatorChar}TestFilesFolder{Path.DirectorySeparatorChar}test.docx"));
    

    Updating data

    To update data you first need to get the data to update, see Get data to learn more on getting data. Once you've data (e.g. you did get a list) you can update the list by changing one of it's properties and then call one of the update methods (e.g. UpdateAsync). You can perform multiple updates to the model and only when you're done with updating the properties you need to call one of the update methods, the PnP Core SDK will keep track of the changes and will only use the actually changed properties in the update request.

    // Get the list to update
    var myList = await context.Web.Lists.GetByTitleAsync("List to update");
    
    // Update a list property
    myList.Description = "PnP Rocks!";
    
    // Update another list property
    myList.EnableVersioning = true;
    
    // Send update to the server
    await myList.UpdateAsync();
    

    Deleting data

    To delete data you do have two options, the first one is deleting the loaded model: like with updating you first need to have the model instance to delete available, see Get data to learn more on getting data. Once you have the model to delete available (e.g. a list item) you can use one of the delete methods to perform the delete. Below example uses the DeleteBatchAsync method to delete all items of a list with a single network request.

    // Assume the fields where not yet loaded, so loading them with the list
    var myList = await context.Web.Lists.GetByTitleAsync("My List", 
                                                        p => p.Title, p => p.Items, 
                                                        p => p.Fields.QueryProperties(p => p.InternalName, 
                                                            p => p.FieldTypeKind,
                                                            p => p.TypeAsString, 
                                                            p => p.Title));
    // Iterate over the retrieved list items
    foreach (var listItem in myList.Items.AsRequested())
    {
        // Delete all the items in "My List" by adding them to a batch
        await listItem.DeleteBatchAsync();
    }
    
    // Execute the batch
    await context.ExecuteAsync();
    

    The second approach does not require you to upfront load the item to delete: model collections that support this type of delete either have DeleteById methods which you simply call and provide the unique id of the item to delete. Below sample shows how to delete a list using this approach.

    // Delete the list with the given id
    await context.Web.Lists.DeleteByIdAsync(new Guid("E623E1A0-6786-498B-99D2-EE6355851CDB"));
    
    // Add the delete of the list with given if to the current batch
    await context.Web.Lists.DeleteByIdBatchAsync(new Guid("E623E1A0-6786-498B-99D2-EE6355851CDB"));
    // Execute the current batch
    await context.ExecuteAsync();
    
    Back to top PnP Core SDK
    Generated by DocFX with Material UI
    spacer