CHUVASH.eu

CHunky Universe of Vigourous Astonishing SHarepoint :)

Tag Archives: extension

Working with resx files in Visual Studio

Today I found a nice Visual Studio Extension for working with localization and resx files: Resx Resource Manager. This extension provides an additional view in your project and scans all the resx files. I would recommend it to all projects where you have to translate your interface. Here is how it looks in my project:

resx-001

It can also assist with some machine translation from Bing and MyMemory:

resx-002

Another good thing is the Export and Import to and from Excel. Wonderful if you need help from Non-developers.

resx-003

Simplify js and css development with Web Essentials (Visual Studio Extension)

If you develop much javascript and css, this is the exension to Visual Studio you just can’t live without: Web Essentials (It is even released for VS2012).

You can do many things with it. Here are two examples for simple but very useful functions:

1. Show which browsers support a css attribute:

2. Collapse javascript functions and create #region areas like in C# code:

There is much more, like less and coffeescript parsing. Just check the documentation. And it is fully appliable in SharePoint development.

Less and Coffeescript

Now using less and coffeescript (update: and typescript) can be really easy even in SharePoint. Whenever you save a .less file or .coffeescript, it will create a corresponding file with .css or .js file extensions in the same folder. So you can write your styles in less and scripts in coffeescript and reference the auto-generated css and js files.

UPDATE: For some reason the css is not generated when I run Visual Studio 2012 on Server 2008 with Web Essentials, but it works fine on my Windows 8 machine. To play around with less, you can use this demo site (creds to @irishbuzz on so).

 

Next: Typescript and SharePoint

Do an unsafe update in a unified manner

Recently I talked about a WithWeb-pattern as described in Jonas Nilssons blog where you can isolate the disposal logic in one place. Another thing is to isolate unsafe updates:

public static class SPWebExtension
{
    public static void UnsafeUpdate(this SPWeb web, Action<SPWeb> action)
    {           
        try
        {
            Log.Info("Trying to do an unsafe update on a web: " + web.Title);
            web.AllowUnsafeUpdates = true;
            action(web);
        }
        catch (Exception e)
        {
            Log.Error(e);
        }
        finally
        {
            web.AllowUnsafeUpdates = false;
        }
    }
}

The Log class is my own class which I presented in my previous post.

Check if user is in group

Use LINQ to check if user is in a group. Create an extension method.

public static bool InGroup(this SPUser user, SPGroup group)
{
  return user.Groups.Cast<SPGroup>()
    .Any(g => g.ID == group.ID);
}

EDIT 2011-01-22: There is a shortcoming of this method. You won’t get a user which is in group through a AD group. You’ll get only users and ad groups. But there is another method to check if a user is inside an AD group.

How could we combine them?…

I think we must start from group this time, not from user:

public static bool HasUser(this SPGroup user, SPUser user)
{
	var users = group.Users.Cast();
	var samAccount = Regex.Replace(user.LoginName, @".*\\(.*)", "$1", RegexOptions.None);
	var exists = users.Any(u => u.LoginName.Equals(user.LoginName));
	if (!exists)
	{
		var ctx = new PrincipalContext(ContextType.Domain);
		foreach (var u in users)
		{
			var login = u.LoginName;
			var groupName = Regex.Replace(login, @".*\\(.*)", "$1", RegexOptions.None);
			var grp = GroupPrincipal.FindByIdentity(ctx, IdentityType.Name, groupName);
			if (grp == null) continue;
			var principals = grp.GetMembers(true);
			exists = principals.Any(p => p.SamAccountName.Equals(samAccount, 
						StringComparison.InvariantCultureIgnoreCase));
			if (exists) break;
		}
	}
	return exists;
}

Using Regex to get the samAccount from loginname is taken from the awesome answer on StackOverflow.

Content Type Id for Image, Audio and Video

After debugging I have found the Content Type Ids for Image, Audio and Video in the assets library. These content type ids are not present in SPBuiltInContentTypeId.

public class SPBuiltInContentTypeIdExtension
{
  public static SPContentTypeId Video =
    new SPContentTypeId
     ("0x0101009148F5A04DDD49CBA7127AADA5FB792B00291D173ECE694D56B19D111489C4369D");
  public static SPContentTypeId Audio =
    new SPContentTypeId
      ("0x0101009148F5A04DDD49CBA7127AADA5FB792B006973ACD696DC4858A76371B2FB2F439A");
  public static SPContentTypeId Image =
    new SPContentTypeId
      ("0x0101009148F5A04DDD49CBA7127AADA5FB792B00AADE34325A8B49CDA8BB4DB53328F214");
}

These three asset content types inherit from Document CT (“0x0101”) and have “0x0101009148F5A04DDD49CBA7127AADA5FB792B” in common, which is the content type id for multimedia content type. So if you want to check if it is a multimedia, use this id.

You can see the content type id when you go to Site Actions – Site Settings – Content Types. Click on a content type you interested in. In the address bar of your browser find ?ctype=.

/_layouts/ManageContentType.aspx?
ctype=0x0101009148F5A04DDD49CBA7127AADA5FB792B00AADE34325A8B49CDA8BB4DB53328F214
&Source=htt....

Eller i koden så kan du göra så här:

var id = properties.Web.AvailableContentTypes
   .BestMatch(properties.ListItem.ContentTypeId);

Enjoy.

Вула Чăвашла

VulaCV - Чăвашла вулаттаракан сайт

Discovering SharePoint

And going crazy doing it

Bram de Jager talking Office 365, SharePoint and Azure

My view and thoughts on Productivity and more.

My programming life

and everything in between

SharePoint Development Lab by @avishnyakov

It is a good place to share some SharePoint stories and development practices.

SharePoint Dragons

Nikander & Margriet on SharePoint

RealActivity - Real-time and trustworthy

Blog site of founder, RealActivty - Paul J. Swider

Mai Omar Desouki - Avid SharePointer

Egyptian & Vodafoner - Senior SharePoint Consultant

Cameron Dwyer | Office 365, SharePoint, Outlook, OnePlace Solutions

Office 365, SharePoint, OnePlace Solutions & Life's Other Little Wonders

paul.tavares

Me and My doings!

Share SharePoint Points!!

By Mohit Vashishtha

Jimmy Janlén "Den Scrummande Konsulten"

Erfarenheter, synpunkter och raljerande om Scrum från Jimmy Janlén

SPJoel

SharePoint for everyone

SharePointRyan

Ryan Dennis is a SharePoint Solution Architect with a passion for SharePoint and PowerShell

SharePoint 2020

The Vision for a Future of Clarity

Aharoni in Unicode, ya mama

Treacle tarts for great justice

... And All That JS

JavaScript, Web Apps and SharePoint