0

Not new to SharePoint but new to SharePoint OnLine.

So, here's the question(s). Is there a way to dynamically create a list using REST,JSON, or C# in SharePoint Online?

I've seen some sites posting code when I did a Google search in JSON and/or C# but in either case I can't figure out how to execute the code or what "shell" to execute from.

I've done this in PowerShell in SP2013 using both REST and PowerShell scripts so maybe it's the same. Seems like a dumb question frankly but I want to learn to create a list from code if it's possible.

Now, I have played with the SP Management Shell a bit but still new to it and perhaps that's the way to do it.

Thanks.

1

Any of the above. If you're accustomed to powershell, an easy way is to get the PNP PowerShell modules installed, which is a helper library where others have written the CSOM code for you.

Once you have it installed, here's the code to create a new list:

New-PnPList -Title "Demo List" -Url "DemoList" -Template Announcements

The docs are here, make sure you read the section on installing and the "getting started" section.

The code snippet above is from the doc for the new-pnplist command, available here.

| improve this answer | |
  • Most helpful! Thanks...I'm off and running on this. – Pablo Sep 15 '19 at 4:44
0

Using REST from C# or just about anything...

URL: https://yourDomain/sites/yourSite/_api/web/lists

Header:

{"accept": "application/json;odata=verbose",
                   "content-type": "application/json;odata=verbose",
                   "content-length":__len__,
                   "X-RequestDigest": "__yourRequestDigest__"}

Body:

{"__metadata": { "type": "SP.List" },
               "AllowContentTypes": true,
               "BaseTemplate": 100,
               "ContentTypesEnabled": true,
               "Description": "My list description",
               "Title": "My List Title" }

The real work here is permissions and/or the RequestDigest token. The "how" depends on where your request originates.

| improve this answer | |
0

Just a bit more information, CSOM also works for such situation.

#Load SharePoint CSOM Assemblies
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"

#Variables for Processing
$SiteURL = "your site url"
$ListName="list name"

#Setup Credentials to connect
$Cred = Get-Credential
$Cred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Cred.UserName,$Cred.Password)

Try {
    #Setup the context
    $Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURL)
    $Ctx.Credentials = $Cred

    #Get All Lists 
    $Lists = $Ctx.Web.Lists
    $Ctx.Load($Lists)
    $Ctx.ExecuteQuery()

    #Check if List doesn't exists already
    if(!($Lists.Title -contains $ListName))
    { 
        #sharepoint online powershell create list
        $ListInfo = New-Object Microsoft.SharePoint.Client.ListCreationInformation
        $ListInfo.Title = $ListName
        $ListInfo.TemplateType = 100 #Custom List
        $List = $Context.Web.Lists.Add($ListInfo)
        $List.Description = "Repository to store project artifacts"
        $List.Update()
        $Ctx.ExecuteQuery()

        write-host  -f Green "New List has been created!"
    }
    else
    {
        Write-Host -f Yellow "List '$ListName' already exists!"
    }
}
Catch {
    write-host -f Red "Error Creating List!" $_.Exception.Message
}

Reference link:https://www.sharepointdiary.com/2014/12/sharepoint-online-powershell-to-create-list.html

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.