1

How to create a list item to SP on premise environment from SP Online. Once a workflow get approved in Online environment, it has to create an item in a on premise SP. how to achieve?!

1

Install and configure the Microsoft Data Management Gateway. From there, you can create a Power Automate flow (formerly Microsoft Flow) which has a Trigger of the appropriate type (e.g. Item Created) and then copies that information to an Action which creates a new item on your SharePoint Server farm.

This will require a Microsoft Flow P1 license for the user making the connection to on-prem.

  • Is it possible to do by any custom coding?! – sam daniel Nov 6 at 15:46
  • Via CSOM, absolutely. You'll spend quite a bit more time developing than then implementing the gateway and using a Flow. – Trevor Seward Nov 6 at 15:48
  • Could you please share any samples on CSOM?! It’s quite urgent task for me and I’m sure it’ll take lot of time to get approval from my org to implement gateway. – sam daniel Nov 6 at 16:12
  • Your best bet is to push with the approvals, then. it takes 5 minutes to install the gateway and another 10 to create the Flow. – Trevor Seward Nov 6 at 16:14
0

Using PowerShell CSOM... very quickly you can do.. you can use the below example as a template :

https://www.c-sharpcorner.com/blogs/create-multiple-items-in-sharepoint-online-list-using-csom-powershell

Here in the above script I am adding list item to sp online.... similarly in the same script we need create one more context for SharePoint on-premise site... using that context get the on-premise list details.... then inside the for each loop of online list items call the add item to on-premise list method.

This also will give right heads on :

https://global-sharepoint.com/2019/11/01/how-to-update-sharepoint-online-document-library-field-value-using-powershell-csom/

So basically you need to create two contexts one for sp online another for sp on-premise... and using the above two examples you can do it very quickly :-)

You can start with the above second script (reference link 2) first which is reading the items from sp online list.... then inside the for each loop of items reading... write another context for sp on-premise here you can inject the logic of the first script (above reference link 1)

Code Sample:

#Setup the Context for SP Online
$Ctx = New-Object Microsoft.SharePoint.Client.ClientContext($SiteURLSPOnline)
$Ctx.Credentials = New-Object 
Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, 
$SecurePassword)

#Get the List
$List = $Ctx.Web.Lists.GetByTitle($ListName)
$Ctx.Load($List)
$Ctx.ExecuteQuery()
$emptyString = ""
#Define CAML Query to get Files from the list in batches
$Query = New-Object Microsoft.SharePoint.Client.CamlQuery
$Query.ViewXml = "@
<View Scope='RecursiveAll'>
    <Query>
         <OrderBy><FieldRef Name='ID' Ascending='True'/></OrderBy>

    </Query>
    <RowLimit>$BatchSize</RowLimit>
</View>"
 $count =0

 #Get List Items in Batches
Do
{

$ListItems = $List.GetItems($Query)
$Ctx.Load($ListItems)
$Ctx.ExecuteQuery()
$ListItems.Count

#Update Postion of the ListItemCollectionPosition
$Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
$Query.ListItemCollectionPosition

If ($ListItems.Count -eq 0) { Break }

#Reading the list Items from SP online.
ForEach($Item in $ListItems)
{
    #Update List Item Title as File Name

    $documentStatus=$Item["DocumentStatus"]
    If($documentStatus -eq "Approved")
    {


         ########The below code add the items to SP - on-premise##############
         try
            {
            $ctxSPOnPremise = New-Object Microsoft.SharePoint.Client.ClientContext($siteURLOnpremise)
            $ctxSPOnPremise.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($spUserName, $securePassword)


            #Get the list
            $spListOnPremise = $ctxSPOnPremise.Web.Lists.GetByTitle($spListNameOnPremise)
            $ctxSPOnPremise.Load($spListOnPremise)
            $ctxSPOnPremise.ExecuteQuery()


              $listItemCreationInformationInSPonPremise = New-Object Microsoft.SharePoint.Client.ListItemCreationInformation
              $newListItemInSPonPremise = $spListOnPremise.AddItem($listItemCreationInformationInSPonPremise)
              $newListItemInSPonPremise["Title"] = $Item["Title"]
              $newListItemInSPonPremise["DocumentStatus"] = $Item["DocumentStatus"] ####and so on...you can add your all your needed column

              $newListItemInSPonPremise.Update()
              $ctxSPOnPremise.ExecuteQuery()

              write-host "Item created in SP On-premise list:";



            }
            catch 
            {

                $errorMessage = $_.Exception.Message +"in adding items to on-premise list using CSOM PowerShell script";
                Write-Host $errorMessage -BackgroundColor Red
                Write-Log $errorMessage 
            }      


         #######adding items to on-premise list is ends here###################




    }


}
Write-Host "============================================================="
Write-Host $count
Write-Host "============================================================="

}While ($Query.ListItemCollectionPosition -ne $null)

Note:

If this is periodically regularly need to be executed - you can automate this execution by configuring this PowerShell script in windows task scheduler as a job.

  • Thanks for your response. I'll try but how to do it as an event handler? – sam daniel Nov 7 at 4:19
  • We can configure this script as a job in Windows task scheduler which will automatically be executed in an interval based on the configuration... this is not event receiver but something like that as an alternate approach... as we can not write event receiver in PowerShell. – Habibur Rahaman Nov 7 at 4:47
  • Ah ok. I need to do something like an event receiver. – sam daniel Nov 7 at 6:11
  • If you don't find event receiver kind of plug in to fit this requirement, then you can make use this code as Windows job as I have mentioned above... configure the job like run every 5 minutes or so... and in the code check for the condition like if the particular item status is equal to approved or closed.. then only create that particular item row in the on premises sp list... in the sample example above handled that scenario :-) – Habibur Rahaman Nov 7 at 7:53

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.