Categories
SharePoint

Creating SharePoint alerts via CSOM

I’m posting this as a useful reminder. It outlines how alerts can now be created via CSOM.

New SharePoint CSOM version released for SharePoint Online – February 2017

The included code sample is in C#, but I’ve translated it to PowerShell – which suits my administrative needs better.

[System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SharePoint.Client" ) | Out-Null
[System.Reflection.Assembly]::LoadWithPartialName( "Microsoft.SharePoint.Client.Runtime" ) | Out-Null


# Set variables
$path = "C:\Temp\Usernames.txt"

$url = "https://xxxxxx.sharepoint.com/sites/help"

$list = "My List"

$title = "$list (Auto-subscribed)"


# Prompt the administrator to log in
$credential = Get-Credential -Credential $null


# Create a connection to SharePoint Online
$context = New-Object Microsoft.SharePoint.Client.ClientContext( $url )
$context.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials( $credential.UserName, $credential.Password )


# Retrieve the targeted web
$web = $context.Web

$context.Load( $web )
$context.ExecuteQuery()


# Loop through the provided usernames and create a new alert for each one
$usernames = Get-Content -Path $path

foreach ( $username in $usernames )
{
    $user = $context.Web.EnsureUser( $username )

    $alert = New-Object Microsoft.SharePoint.Client.AlertCreationInformation
    $alert.List = $context.Web.Lists.GetByTitle( $list )
    $alert.AlertFrequency = [Microsoft.SharePoint.Client.AlertFrequency]::Daily
    $alert.AlertTime = ( Get-Date ).AddDays( 1 )
    $alert.AlertType = [Microsoft.SharePoint.Client.AlertType]::List
    $alert.AlwaysNotify = $false
    $alert.DeliveryChannels = [Microsoft.SharePoint.Client.AlertDeliveryChannel]::Email
    $alert.Status = [Microsoft.SharePoint.Client.AlertStatus]::On
    $alert.Title = $title
    $alert.User = $user
    $alert.EventType = [Microsoft.SharePoint.Client.AlertEventType]::All
    $alert.Filter = "1"

    $guid = $user.Alerts.Add( $alert )

    $user.Update()

    $context.ExecuteQuery()
}
Categories
Coding Server

Hiding SharePoint WebParts via Code

Another note to myself…

When wanting to programmatically hide a WebPart, don’t try to set it’s Visible property to False. This will cause you lots of trouble and create errrors. The correct method is to set its Hidden property to True.

if (items.Count == 0)
{
   this.Hidden = true;
   return;
}

 

Categories
Coding

Rounding to nearest X

An interesting forumla to remember for rounding numbers to the nearest X.

Int32 value = 43.7;
Int32 toNearest = 5;
Int32 result = Math.Round(value / toNearest) * toNearest;

Categories
Design & UI

SharePoint: University College Falmouth Intranet

These design solutions were created as part of the University’s move from a 10 year old, bespoke, Classic ASP driven Intranet (which I originally developed) to Microsoft SharePoint 2010. Their purpose was to act as a starting point for discussion among staff and students and to generate feedback.

Homepage

Sub Page

Categories
Server

SPListItem URLs

There seems to be no built-in function or property to quickly return the correct URL for a SharePoint list item. The obvious property, SPListItem.URL, always returns an invalid URL along the lines of http://sharepoint/Lists/MyList/155_.000. Not ideal.

To get around this and return a correctly formatted web address, you can use the following code:

item.Web.Url + "/" + item.ParentList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url + "?ID=" + item.ID

Categories
Coding

Looping through a SubSonic collection

ArticleCollection articles = new ArticleCollection();
articles.Where("IssueID", 10);
articles.Load();

if (articles.Count > 0)
{
foreach (Article a in articles)
{
Response.Write(a.Headline + ", ");
}
}

Categories
Coding

Mapping SQL Server integer types to .NET types

BigInt = Int64 (Long)
Int = Int32 (Integer)
SmallInt = Int16 (Short)
TinyInt = Byte

e.g. byte recurrenceType = reader.GetByte(8);

Categories
Coding

Copying data to the clipboard using C#

Clipboard.SetText( “Hello World!”);

Short and sweet! 🙂

Categories
Coding

Converting C# to VB.Net

Thanks to a reply to one of my posts within the SubSonic forums, I was pointed to this very nifty utility at Developer Fusion – a tool that will automatically convert C# code to VB.Net and back again.

Categories
Coding

The ASP.Net Web Config File

Posting this for future reference…