CHUVASH.eu
  • About
  • Search

Posts

December 1, 2011

Get Distinguished Name for a user

To get the distinguished name for a user, it isn’t enough to get an SPUser object. The distinguished name is the unique string for identifying a user in Active Directory (eg. CN=BeforeDAfter,OU=Test,DC=North America,DC=Fabrikam,DC=COM) Even using UserProfile object is not that clear. The distinguished name can be found in a property which can be retrieved with brackets: up[PropertyConstants.DistinguishedName]

public static string GetDistinguishedName(string login)
{
   var dn = "";
   UserProfile up;
   using (var site = new SPSite("http://dev"))
   {
      var serviceContext = SPServiceContext.GetContext(site);
      var upm = new UserProfileManager(serviceContext);
      var exists = upm.UserExists(login);
      if (!exists)
         upm.CreateUserProfile(login);
      if (exists)
      {
         up = upm.GetUserProfile(login);
         dn = up\[PropertyConstants.DistinguishedName\].Value.ToString();
      }
   }
   return dn;
}
```The code is simplified and doesn't contain any error handling. And a better handling of upm.UserExists must be implemented: If upm.CreateUserProfile(login) runs, it doesn't make it so quickly and the next step won't run (upm.GetUserProfile). If you are not working in SP Context, you can see the distinguished name for a user in Powershell:

import-module activedirectory $u = get-aduser administrator $u.DistinguishedName

read more
November 23, 2011

css3 transform

See Richards Bradshaw’s page with explanations and examples of css3 transform and transitions. His code is also available for forking on Github.

read more
November 22, 2011

jQuery timeago and the localization

jQuery timeagoIf you have used SPUtility.TimeDeltaAsString you must know how useful it is. There is also the jQuery plugin that can perform counting of time deltas and (!) translate it. jQuery timeago is available for many languages, Swedish among others:

// Swedish
jQuery.timeago.settings.strings = {
  prefixAgo: "för",
  prefixFromNow: "om",
  suffixAgo: "sedan",
  suffixFromNow: "",
  seconds: "mindre än en minut",
  minute: "ungefär en minut",
  minutes: "%d minuter",
  hour: "ungefär en timme",
  hours: "ungefär %d timmar",
  day: "en dag",
  days: "%d dagar",
  month: "ungefär en månad",
  months: "%d månader",
  year: "ungefär ett år",
  years: "%d år"
};
```The code is hosted on [Github](https://github.com/rmm5t/jquery-timeago). In order the time deltas to be localized in SharePoint a ScriptLink has to be used:

<SharePoint:ScriptLink ID=“timeagoLanguage” runat=“server” Language=“javascript” Name=“jquery.timeago.lang.js” Localizable=“True” LoadAfterUI=“True” OnDemand=“False” ></SharePoint:ScriptLink>

read more
November 22, 2011

Filtering in javascript

The simplest and the best way to filter an array is to extend the Array.prototype as described here:

if (!Array.prototype.filter) {
    Array.prototype.filter = function (func) {
        var len = this.length;
        if (typeof func != "function")
            throw new TypeError();

        var res = new Array();
        var thisp = arguments\[1\];
        for (var i = 0; i < len; i++) {
            if (i in this) {
                var val = this\[i\];
                if (func.call(thisp, val, i, this))
                    res.push(val);
            }
        }

        return res;
    };
}

```Then let's filter on [titles](/2011/11/21/sorting-dates-in-javascript/ "See an example for Sorting with Titles"), which [start with](http://stackoverflow.com/questions/646628/javascript-startswith "See the solution for startsWith in javascript on Stackoverflow") "N":

function startsWithN(element, index, array) { return element.toLowerCase().indexOf(“n”) == 0; }

read more
November 21, 2011

Extend an event in javascript

If you use jQuery, you don’t need it. But if you for some reason must use javascript you must beware of adding events. The most dangerous is window.onload. Consider this scenario:

function doSomethingGreat() {};
window.onload = doSomethingGreat;
```It works fine if you are the only person who writes window.onload handler. But on such platforms like SharePoint you can't know which events exist. And if you just write **window.onload = doSomethingGreat;** you override all other window.onload events. Again, in jQuery you have nothing to worry. jQuery(window).load(doSomethingGreat) will just add your event, not override. In this post I'll show how to extend an event handler the pure javascript way. We have to create our own function for adding event handlers. First the old events have to be temporary saved and then the new event has to be added. [Like this](http://jsfiddle.net/mirontoli/cnN6s/):

function addOnclick(elem, func) { var old = elem.onclick; if (typeof elem.onclick != ‘function’) { elem.onclick = func; } else { elem.onclick = function () { if (old) { old(); } func(); };
} }

read more
November 21, 2011

Sorting Dates in javascript

To sort an array in javascript, just run sort():

array = \["skåne", "blekinge", "halland"\];
array.sort();
```To [perform a more intelligent sorting](http://stackoverflow.com/questions/3859239/sort-json-by-date "See the original answer in stackoverflow") we can define our sorting logic. What if an array has objects with different properties? For example, an object has title and lastupdated. We want to sort on lastupdated. Just create a function for this with two parameters:

function custom_sort(a, b) { return new Date(a.lastUpdated).getTime() - new Date(b.lastUpdated).getTime(); }

read more
November 15, 2011

Google Analytics in Sharepoint

Google Analytics is a popular and mature tool for monitoring network activities on your site. Why not use it in Sharepoint? Dave Coleman and Mike Knowles provide good guides for doing that. There is a promising plugin to SP SPGoogleAnalytics which integrates both and provides an easy interface to put GA to your SharePoint installation and see the data directly on your intranet. It comes from codeplex: What do you think about spgoogleanalytics.codeplex? When I recently had to turn on Google Analytics on an intranet, I had no time for evaluating SPGoogleAnalytics, so I did the harder way: just put ga script in the end of a master page, just before ending tag. But what if one has multiple masterpages? Is there a way to put GA in one step? Maybe a user control like Knowles suggests?

read more
November 2, 2011

Subdomains for site collections

In order to be able to use subdomains for different sitecollections, we have to use hostheaders. Sharad Kumar provides a good example:

$w = Get-SPWebApplication http://sitename
New-SPSite http://www.contoso.com 
    -OwnerAlias "DOMAIN\\jdoe" -HostHeaderWebApplication $w 
    -Name "Contoso" -Template "STS#0"
```If we want to create, say http://rockets.contoso.com, we can run:

New-SPSite http://rockets.contoso.com -OwnerAlias “DOMAIN\jdoe” -HostHeaderWebApplication “http://sitename” -Name “Rockets” -Template “STS#0” -Language 1053

restart-service iisadmin

read more
October 31, 2011

Bootstrap and Sharepoint

Twitter Bootstrap is awesome, based on less.js,  ust add this line in your html code:

<link rel="stylesheet" href="http://twitter.github.com/bootstrap/1.3.0/bootstrap.min.css">

And your page looks already good. If you use css classes like btn label warning danger and much more, you get a right design directly out of the box. Now I want to test it in Sharepoint. Will it work? What do you think? jQuery Theme Roller. Another interesting resources are jQuery ThemeRoller. WebResources Depot:

read more
October 25, 2011

Working with web.Properties

Sometimes one may need more properties to track on a SPWeb beside Title and Description. One of the possibilities is to create a custom list (maybe a hidden one) with keys and values (Title and Value). It works fine. The good thing with it is also the possibility to change the key-value pair directly in the web interface. Another approach is to use web.Properties which is a Dictionary with key-values pairs. A simpler and neater solution: Here is a good motivation from the best sharepoint book Sharepoint as a Development Platform (p. 1043):

read more
  • ««
  • «
  • 31
  • 32
  • 33
  • 34
  • 35
  • »
  • »»
© CHUVASH.eu 2025