Posts tagged with "C#"

Creating SharePoint alerts via CSOM

Posted within Administration Development on by Ryan Ball

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()
}

Hiding SharePoint WebParts via Code

Posted within Administration Development on by Cornflower Design

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;
}

 

Rounding to nearest X

Posted within Development on by Ryan Ball

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;

SharePoint: University College Falmouth Intranet

Posted within Doodles on by Cornflower Design

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

SPListItem URLs

Posted within Administration on by Cornflower Design

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

Looping through a SubSonic collection

Posted within Development on by Ryan Ball

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

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

Setting AM & PM Designators

Posted within Development on by Ryan Ball

This is a useful little tip. By default, AM and PM time designators are output in uppercase (in the 1033 language set atleast) but there are times (in this case, enforced by a coporate style guide) when they should be lowercase. There are two ways to achieve this – one is…

DateTime now = DateTime.Now;
String nowString = now.ToString("dddd, d MMMM yyyy a\\t h.mm");
nowString = nowString + now.ToString("tt").ToLower();

… which is okay if you’re just using it the once. More than once, you’re better off using a DateTimeFormatInfo to set the AM and PM designators. Like so…

DateTimeFormatInfo dtfi = new DateTimeFormatInfo();
dtfi.AMDesignator = "am";
dtfi.PMDesignator = "pm";

String nowString = now.ToString("dddd, d MMMM yyyy a\\t h.mmtt", dtfi);