0

I have the following script to get the count of custom user site actions on my SharePoint online site:

[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")

$siteUrl = "https://mytenant.com"
$username = Read-Host -Prompt "Enter user login name"
$SecurePassword = Read-Host -Prompt "Enter user login password" -AsSecureString
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)

$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $SecurePassword)
$ctx.Credentials = $credentials

$rootWeb = $ctx.Site
$ctx.Load($rootWeb)
$ctx.ExecuteQuery()
$caColl = $rootWeb.get_userCustomActions()
$ctx.Load($caColl)
$ctx.ExecuteQuery() 

Write-Host 'Total number of custom actions: '$caColl.Count

This is C# CSOM code in PowerShell. The function get_userCustomActions() is a function in the SP.js library. How is it possible that this script works? When I use this code in Visual Studio I can't build because get_userCustomActions() is a non existing method on the Site or web object in the SharePoint.Client.dll

What is happening here?

2
  • Can you try $rootWeb.UserCustomActions simple Nov 29, 2017 at 13:32
  • 1
    That also works but I am trying to figure out why get_userCustomActions() works in this powershell script and not in a client console application.
    – Danny
    Nov 29, 2017 at 13:34

1 Answer 1

0

This is by design. .NET Managed CSOM library will have the property named UserCustomActions: enter image description here

While Js library will have the method named get_userCustomActions

<script type="text/ecmascript">

function runCode() {
    var clientContext = new SP.ClientContext();
    var site = clientContext.get_web();
    console.log(site);
    var UserCustomActions = site.get_userCustomActions();

    var newUserCustomAction = UserCustomActions.add();
    newUserCustomAction.set_location('Microsoft.SharePoint.StandardMenu');
    newUserCustomAction.set_group('SiteActions');
    newUserCustomAction.set_sequence(1000);
    newUserCustomAction.set_title('New Menu Item');
    newUserCustomAction.set_imageUrl('/_layouts/images/myIcon.jpg');
    newUserCustomAction.set_description('Menu item added via ECMAScript');
    newUserCustomAction.set_url('/_layouts/create.aspx');
    newUserCustomAction.update();
    clientContext.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed));
}

function onQuerySucceeded(sender, args) {
    alert('New menu item added to Site Actions menu.\n\nTo view the new menu item, refresh the page.');
}

function onQueryFailed(sender, args) {
    alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}

</script>

enter image description here

Both of them will return user custom actions collection, same functionality but different name.

3
  • But how is it possible that the method get_userCustomActions() works in my C# code. According to your example I should use the property UserCustomActions which also works. But get_userCustomActions() also works. So am I executing javascript in my C# code?
    – Danny
    Dec 2, 2017 at 21:12
  • @Danny c# and JavaScript is different, in C#, this is worked with UserCustomActions property which will return all custom actions collection. So in the C#, you need to use UserCustomActions rather than get_UserCustomActions, Did this answer your concern ?
    – Jerry
    Dec 3, 2017 at 10:03
  • I am just wondering why the JS function get_UserCustomActions works in my c# code. I would expect an error saying it does not exist. In Visual Studio I get that error message, inside the powershell script it gives no error and even produces a correct result
    – Danny
    Dec 4, 2017 at 9:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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