Monthly Archives: August 2014

SharePoint – Use JQuery to set Column Width

Hello everyone. Today I was asked to alter the column width of some SharePoint Web Parts to improve the look and feel of a site. I altered the column width with a simple Javascript and JQuery.

Here is an example how the Web Parts of the site looked before I added my script:

sharepoint_before_column_width_script

Not pretty but luckily there is a simple way to change that.

Here is the script code:

<script src="/sites/test/Scripts/jquery.min.js"></script>
<script language="javascript">
$(function(){
$("TH.ms-vh2:contains('Name')").css("width", "300px");
});
</script>

With the above script you can alter the width of any column you want. You simply need to replace “Name” in the script with the Display Name of your Column.

To use the script you need to add a Content Editor Web Part to your site and embed your script in it.

sharepoint_add_column_width_script

After you have applied your script to the site your site will look like this:

sharepoint_after_column_width_script

Looks better than before, right? Well, that´s it. That´s all you need to do. I hope you liked my post. See you next time.

Sources:

http://social.technet.microsoft.com/Forums/sharepoint/en-US/eab8a3ca-1a91-45ca-b7d5-a5f78592083e/adjust-column-width-in-sharepoint-2013?forum=sharepointgeneral

http://www.emmielewis.com/changesplistcolumnwidth/

Tagged ,

SharePoint – Event Receiver to prevent documents from being deleted

Hello everyone. A few days ago I got a request to find a solution for the following problem: My customer has some Document Libraries where he stores important documents. These documents mustn´t be deleted since those documents are important contracts. So what can you do to prevent this?

Well there are some ways to achieve this: You can create a new Permission Level which does not allow the Delete action or you can create an Event Receiver which cancels the Delete action and throws an error message. I chose the second option since my customer and I both wanted to provide the user some information why his Delete action was canceled. (The first option would simply grey out the Delete action possibly resulting in users thinking there is something wrong with the software system.)

To create the Event Receiver do the following:

First open up Visual Studio on your Development machine and choose New Project. Select Event Receiver as your project template.

sharepoint_deleting_event_receiver

Next I chose Deploy as a sandboxed solution. (A sandboxed solution is enough for our planned actions.)

sharepoint_deleting_event_receiver_sandbox

Next select Document Library from the Dropdown and check An item is being deleted as event.

sharepoint_deleting_event_receiver_settings

And here is the code:

using System;
using System.Security.Permissions;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.Workflow;

namespace SPDeletedReceiver.EventReceiver1
{
/// <summary>
/// List Item Events
/// </summary>
public class EventReceiver1 : SPItemEventReceiver
{
/// <summary>
/// An item is being deleted.
/// </summary>
public override void ItemDeleting(SPItemEventProperties properties)
{
base.ItemDeleting(properties);

using(SPWeb web = properties.Web)
{
SPList list = web.Lists["DoNotAllowDelete"];
SPQuery query = new SPQuery();
query.Query = string.Format("<Where><Eq><FieldRef Name='Title' /><Value Type='Text'>" + properties.ListTitle + "</Value></Eq></Where>");
SPListItemCollection items = list.GetItems(query);

if(items.Count > 0)
{
properties.ErrorMessage = "You are not allowed to delete items.";
properties.Status = SPEventReceiverStatus.CancelWithError;
properties.Cancel = true;
}
}
}
}
}

Code Explanation: The actions of the code are not very complex. What it simply does is check if the List Title of the List where the Delete action was triggered is listed in a Custom List called “DoNotAllowDelete”. If the List Title is listed the Delete action will be canceled. If not the Delete action will be executed.

So you are basically black listing some Document Libraries where the Delete action is forbidden for everyone.

To use the Event Receiver you need to upload the solution wsp file to your Solutions in your Site Collection and activate it. And you must activate the Event Receivers feature on the sites (Site features) where you want to use the solution.

Important: You also need to create a Custom List called “DoNotAllowDelete” on each Site you want to use the solution. And you need to enter the Library Titles of your black listed Document Libraries into the Title column of this list.

Here is an example picture of my “DoNotAllowDelete” list:

sharepoint_event_receiver_donotallowdelete

As you can see I black listed the Document Library “Documents”. If I now try to delete a document is the “Documents” library I will receive an error message like you can see below and the the document will not be deleted.

sharepoint_deleting_event_receiver_error_message

Well that´s it. I hope my post was interesting for you. See you next time.

As always you can download my code from here.

Sources:

http://planetlotus.org/profiles/bram-nuyts_116783_prevent-items-from-being-deleted-using-an-event-receiver

Tagged ,

Get/Set Active Directory Photos by using PowerShell

Hello everyone. Today a colleague asked me if I know of a simple way to get user pictures from Active Directory. Of course I do and I will show you how you can get and set pictures in Active Directory by using PowerShell.

Important: The following scripts require Remote Server Administration Tools to be installed on your computer if you do not run them on a Domain Controller.

To get pictures from a user in Active Directory simply run the script below and provide a UserName and a Path (For example: C:\example.jpg) where the picture should be stored:

Param([parameter(Mandatory=$true)][alias("User")]$UserName, [parameter(Mandatory=$true)][alias("Picture")]$PicturePath)

Import-Module ActiveDirectory

$user = Get-ADUser $UserName -Properties thumbnailPhoto
$user.thumbnailPhoto | Set-Content $PicturePath -Encoding byte

To update a users photo simply run the script below and provide a UserName and Path of the new picture:

Param([parameter(Mandatory=$true)][alias("User")]$UserName, [parameter(Mandatory=$true)][alias("Picture")]$PicturePath)

Import-Module ActiveDirectory

$photo = [byte[]](Get-Content $PicturePath -Encoding byte)
Set-ADUser $UserName -Replace @{thumbnailPhoto=$photo}

That´s it. You can get/set pictures in Active Directory as simple as this.

As always you can download my scripts from here.

Sources:

http://241931348f64b1d1.wordpress.com/2011/05/12/how-to-put-and-retrive-active-directory-photo-attribute/

 

Tagged ,

Group Policy – Use Regional Settings to change Date Format

Hello everyone. Today I want to share some knowledge with you which might be useful if you need to change the regional settings of your customers. Today I had the requirement to change the American Date format (MM/dd/yyyy) to the ISO8601 format (yyyy-MM-dd).

Configure Group Policy

First open Group Policy Management and go to User Configuration -> Preferences -> Control Panel Settings -> Regional Options.

group_policy_regional_options

Next right click on the area and choose New -> Regional Options.

Now go to Date and set the following values:

  • Short date format: yyyy-MM-dd
  • Date separator:

group_policy_new_regional_option_before

Now the important part: Go through all tabs and press F5. You will notice that all red lines will change into green lines.

group_policy_new_regional_option_after

Now go to the Common tab and click on OK.

It is important to go through all tabs and to press F5 otherwise the settings will not apply. I do not know why but I found this solution here.

Apply the settings to your Clients

To apply the settings simply do the following things:

  • Restart your Client machine

Or

Run cmd as Administrator and run the following command:

gpupdate /force

After the command executed successfully you need to Log off and Log on again on your Client machine to apply the settings.

The result will look like this:

group_policy_regional_options_client_date_format

That´s it. You have successfully changed the date format.

I wanted to share this information since I wasted quite some time searching for a solution why my settings would not apply. I hope this post is useful for you. See you next time.

Sources:

http://www.frickelsoft.net/blog/?p=126

http://technet.microsoft.com/en-us/library/cc754020.aspx

Tagged