Wednesday, February 10, 2016

Add Comic book web part

On our internal SharePoint server we have IT sub site and on default page we have Image Viewer Web part which shows Dilbert comic images. From time to time we change comic image, and we used to do it by hand, changing image URL manually.

I got tired of changing images manually, so I created small script which will do hard work instead of me. You may ask: "Why do this when you have already created web parts, which will do exactly the same thing?" Well, for one thing, some companies prohibit installation of web parts from unknown sources. Also, this script can be recycled and used for some other purposes. For instance, you can add pictures with security tips and advises on your SharePoint default page, and then use this script to change it in regular intervals. And last, I wanted to see how difficult it is to create such a script :).

It turned out it is not that difficult. I will explain all necessary steps:

1. Create document library for images; You can create new document library to store images, or you can use existing one. On our site I used existing library called Images, just created folder Dilbert in it. There I put Dilbert comic images.

2. Edit site default page, add Image Viewer web part in it (Page > Edit Page > Add Web Part > Media and Content > Image Viewer).

3. Change a title of web part to Dilbert, you can also add some image for testing purposes, and change Alternative Text to Dilbert. Of course, if you want, you can customize this web part according to your desires.

4. Save Add-DilbertComic.ps1 script on some location on SharePoint server (for instance "C:\ScheduledTasks"). You need to change web site URL, library name and web part title according to your configuration. Here is an example how script should look like:

#This script randomly takes pictures from document library and place it in webpart on default site page
# Created by Krsto Savic

Add-PSSnapin Microsoft.SharePoint.Powershell
$web = get-spweb http://ift.tt/20opMKE
$list = $web.Lists | where {$_.title -eq "Images"}
$PicUrl = ($list.items | where {$_.file -like "Images/Dilbert/*"} | get-random).url
$page = $web.GetFile(“default.aspx”)
$page.CheckOut()
$wpm = $web.GetLimitedWebPartManager($page.Url, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
$wp = $wpm.WebParts | Where-Object { $_.Title -eq “Dilbert” }
$wp.ImageLink = “/it/$PicUrl"
$wpm.SaveChanges($wp)
$page.CheckIn(“Changed Dilbert Webpart”,[Microsoft.SharePoint.SPCheckinType]::MajorCheckIn)
$web.Close()

After this, you need to test the script. Open SharePoint Management Shell and start the script to see if the pictures are changing in web part. Of course, I don't have to tell you that you need to run Management Shell as Shell administrator, and to open Shell using Run as Administrator. Right?

5. Once you tested script, all what is left is to configure Scheduled task to run this script on regular intervals:

  1. On the system that the task will be run from, open the Windows Task Scheduler. This can be found in the Start menu, under Start > Administrative Tools.

  2. In the Task Scheduler, select the Create Task option under the Actions heading on the right-hand side.

  3. Enter a name for the task (for instance Change Dilbert comic), and give it a description (the description is optional and not required).

  4. In the General tab, go to the Security options heading and specify the user account that the task should be run under. Change the settings so the task will run if the user is logged in or not. It is best to use account with Shell administrator rights, and with password never expires option configured. You can use SharePoint farm account, or some other SharePoint admin account. 

  5. Next, select the Triggers tab, and click New to add a new trigger for the scheduled task. This new task should use the On a schedule option. The start date can be set to a desired time, and the frequency and duration of the task can be set based on your specific needs. For instance, set it to run weekly, every Sunday at 11pm. In that way, every Monday users will have fresh copy of comic to read. Click OK when your desired settings are entered.

  6. Next, go to the Actions tab and click New to set the action for this task to run. Set the Action to Start a program.

  7. In the Program/script box enter "PowerShell."

    In the Add arguments (optional) box enter the value ".\[Your PowerShell Script Name]." For example, if your PowerShell Script is named "Add-DilbertComic.ps1" then you would enter ".\Add-DilbertComic.ps1" as the value.

    Then, in the Start in (optional) box, add the location of the folder that contains your PowerShell script. In this example, the script is in a folder called "ScheduledTasks" that is off the root C: drive.

    Note: The location used in the Start in box will also be used for storing the scheduled task run times, the job history for the copies, and any additional logging that may occur.

    Click OK when all the desired settings are made.

  8. Next, set any other desired settings in the Conditions and Settings tabs. You can also set up additional actions, such as emailing an Administrator each time the script is run.

  9. Once all the desired actions have been made (or added), click OK. The task will be immediately set, and is ready to run.

  10. Try ti run task manually to see if it is working.

And that's it. You have your own custom web part which change comic book picture every week.

Add-DilbertComic.ps1


by Krsto Savic via Everyone's Blog Posts - SharePoint Community

No comments:

Post a Comment