Category Archives: Sharepoint

Sharepoint 2013 – some thoughts about sticky footer the sharepoint resize event and DOM breakpoints

Lately on a SP 2013 project where we had a sticky footer, we were facing a problem lots of people currently face: SharePoint calculates the height (and width by the way) of the s4-workspace div based on the window object size.

It means that the workspace does not consider the footer, and when the content needs a scroll, the scroll is hidden behind the footer.. not cool :|

After spending some time and hitting the wall, I got a solution that’s not too ugly..

1st try
Add an event listener to the window resize event and subtract from the s4-workspace height the height of the footer.  And it worked…. for a second only.. then the SP event that recalculates the size would override it again. It was a dead end.

2nd try
It become obvious that I needed to understand what method was being called by SharePoint to calculate the sizes. But I didn’t find a way to see all the event listeners attached to the window element.

Then I looked at the problem from another angle. What do I know?

I know SharePoint is changing the size of the s4-workplace div. How can I find where and how does he do that? The answer is the DOM breakpoints.

Using Chrome developer tools, I selected the s4-workspace div and the selected “Break On…”, “Attributes modifications”:

dom breakpoints

Then I resized the browser window and the code stopped on the exact spot where a modification for the workspace div attributes was operated. The function happened to be on the global scope, and the only function on the call stack:

dom-breakpoint-stack

So now that I know that the name of the function that SharePoint uses to calculate the size of the workspace div, I can remove the height of the ribbon. I did it by saving the original function to a variable, and replacing the function with a new one that calls the original one and right after removes the footer height from the workspace div:

var originalResizeFunction = FixRibbonAndWorkspaceDimensions;
FixRibbonAndWorkspaceDimensions = function()
{
    //let sharepoint do its thing
    originalResizeFunction();

    //adapt to the footer size (assuming  #footer is the footer element)
    $("#s4-workspace").height($("#s4-workspace").height() - $("#footer").height()
}

Remember to call this script after the init.js of SharePoint is loaded. Do it either at the end of the DOM or use the ExecuteOrDelayUntillScriptLoaded method.

Now the workspace adapts perfectly to the footer and the scrollable content does not hide behind it.

Cheers!

[Javascript] – Parsing integers with trailing “0” with parseInt

We are always learning, sometimes not the best way :)

As most of the times, when I develop in JavaScript, I end up using the chrome debugger as it’s lighter than its IE counterpart.

On a project, I had to retrieve some dates from DOM elements and process them, place the elements in the proper position.

The dates were on the following format: “MM:YY”. It means that February is “02” and September is “09”.

While on chrome the parsing of those texts were ok:
parseInt(“02″) -> 2
parseIng(“09″) -> 9
on IE9 and IE11 they were inconsistent:
parseInt(“02″) -> 2
parseInt(“09″) -> 0

Apparently parseInt works in OCTAL mode by default when the string starts with a “0”. Since in octal mode 8 and 9 don’t exist, it returns 0, for IE 8 Document mode.

Like it or not, it is designed like that, when you use IE 8 Document Mode (which you necessarily use for sharepoint 2010).

JsBin (set your browser to IE 8 Document Mode and ignore the JSBIN javascript errors caused it causes):
JsBIN

So, it’s necessary to force the decimal mode:
parseInt(“09″,10) -> 9

So from now on, I’ll always specify the decimal mode, just in case.

Add a security trimmed node to Sharepoint top bar navigation

After programatically provisioning a new SPWeb object, it is sometimes necessary to create a new entry on the root web top bar menu.
So far so good, but what happens if the subsite has different permissions from the root site. What if someone with access to the root site sees the menu entry for a site that he’s not entitled to see?

The following piece of code allows the creation of the menu node with swcurity trimming: only the users with permissions on the sub-sites will see the nodes on the menu.

Lets take for example the baseWeb onject as the root SPWeb and the provisionedWeb object as the recent provisioned one.

public void aaa()
{
        var menuNodes= baseWeb.Navigation.TopNavigationBar;
	// Check for an existing link to the subsite.
	SPNavigationNode newNode = menuNodes
		.Cast<SPNavigationNode>()
		.FirstOrDefault(n => n.Url.Equals(siteNew.ServerRelativeUrl));
	
	// No link, so add one.
	if (newNode == null)
	{
		// Create the node.
		newNode = new SPNavigationNode(provisionedWeb .Title, provisionedWeb .ServerRelativeUrl);
		// Add it to the collection.
		newNode = menuNodes.AddAsLast(newNode);
	}                                   
}

[Sharepoint] Adding a field to a list view with code -> don’t use the DefaultView property

Today we faced a strange problem doing  a task as simple as adding a field to the default view (“All items”) with code.
The code would run inside a feature activation and it was something like this:

 var view = list.DefaultView;
 view.ViewFields.Add(fieldName);
 view.Update();

But… even after the update, the viewfields property remained unchanged…. The view reference was pointing to “All Items”, which was correct, all the existing viewfields were retrieved correctly, but we were unable to add one.


The next logical thing to do was to reference view differently. Referencing it by it’s name did the job. So the working code doesn’t change a lot:

 var view = list.Views["All Items"];
 view.ViewFields.Add(fieldName);
 view.Update();

But still it made us loose precious minutes of our time to get to the solution…
Hope it’s useful to you!

Cheers

[Sharepoint] Target a single organizational unit of the Active Directory from a Site Collection

One of these days I had a challenging request: create a “multi-tenant” sharepoint app using a single instance and the same Active Directory.

This application was supposed to allow several companies to use a specific part of it.

For scalabilty reasons it was decided that this segregation could not be done at web application level – what would have made things easier- so the separation will be done at site collection level.

Continue reading “[Sharepoint] Target a single organizational unit of the Active Directory from a Site Collection” »

Sharepoint group description – add link or other HTML

One of these days I needed to provide rich sharepoint group descriptions, with html, and even a link.

Setting the description directly on the SPGroup object had the HTML being escaped and displayed on the group list page, which was not the wanted result.

So, how to get the rich description, like on OOTB sharepoint groups?
Continue reading “Sharepoint group description – add link or other HTML” »

[Sharepoint] Hide QuickLauch on feature activation

Sometimes, when applying branding to Sharepoint sites, it’s necessary to hide the quicklaunch bar.

Some people do that by overriding the css styles, and basically making it invisible. This is a good approach for when, on certain pages, we need to display the quicklaunch, while on the others we need to hide it.

Please note that the quicklaunch it’s always there, with this approach. It’s simply not visible.

For a situation where we’ll never want to display the quicklaunch, we can simply deactivate it, with a feature.

Let’s say we create a feature called “HideQuickLaunch”, scoped for a site (Web). On the event receiver we can override the following methods:


//disable quicklaunch on feature activation
public override void FeatureActivated(SPFeatureReceiverProperties properties)
{
    using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
    {
        currentWeb.AllowUnsafeUpdates = true;
        currentWeb.QuickLaunchEnabled = false;
        currentWeb.Update();
        currentWeb.AllowUnsafeUpdates = false;
    }
} 

//enable quicklaunch on feature deactivation
public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
{
    using (SPWeb currentWeb = (SPWeb)properties.Feature.Parent)
    {
        currentWeb.AllowUnsafeUpdates = true;
        currentWeb.QuickLaunchEnabled = false;
        currentWeb.Update();
        currentWeb.AllowUnsafeUpdates = false;
    }
} 


Hope it helps ;)

[Sharepoint] How to get the MySiteHost Url with code

Sometimes we need to reference the MySiteHost, to create a link or for any other reason.

To avoid hard-coding it or saving it on a resource file (which would need redeployment to change), we can use the UserProfileManager and retrieve it dynamically:

 public static string GetMySiteHostUrl()
 {
     SPServiceContext context = SPServiceContext.GetContext(SPContext.Current.Web.Site);
     UserProfileManager upm = new UserProfileManager(context);
     return upm.MySiteHostUrl;
  }

Note: It’s necessary to reference the Microsoft.Office.Server.UserProfiles Asssembly.

Cheers.

[Sharepoint] – DataContext with custom URL

Hello,

One of these days I had to do some bugfixing on a Sharepoint project which used the Microsoft.SharePoint.Linq.DataContext class.

While trying to access a SPList belonging to Site Collection another Site Collection I always had an “Invalid Url Exception” right from the contructor, and other times I had exceptions when accessing the objects.

var context  = new DataContext(otherSiteColUrl);
 


The problem is that inside, the constructor, the URL is only taken into account if we have no HttpContext. If we do have one, the context URL (which means, the current site url) the custom url passed on the constructor is ignored.
Continue reading “[Sharepoint] – DataContext with custom URL” »