Inherits Cloud

Loving Microsoft Cloud, specially Office 365

Autor: lmanez (página 3 de 15)

Using the OOB Taxonomy picker control just from JavaScript code in SharePoint Online

In this blog post I’m gonna show you how to use the OOB Taxonomy picker control without any server side control or code, just using JavaScript.

As you probably know, the Taxonomy picker control is the TaxonomyWebTaggingControl class, that looks like:

image image

However, all the core functionality of this control is actually done by JavaScript so, in some cases, can be useful how to reuse all of this functionality without any server side stuff, for example, imagine you can add a Taxonomy picker control using a Script editor web part, letting the user pick a Term and then execute some custom JS code.

If someone is curious, I got all this knowledge with some reflection from the TaxonomyWebTaggingControl class, and some hours reading a lot of JS code that SharePoint generates when you work with Taxonomy fields (no magic Smile)

First thing you need to know is that almos all of the Taxonomy picker control functionality is inside the Script “scriptforwebtaggingui.js”, and if you are working with Taxonomy, likely you’ll also need the file “sp.taxonomy.js”, so, ensure you have a reference to this files an option to this could be using the ScriptLink, if you can edit the MasterPage:




or, you can use the SP.SOD framework. Here is the JS code generated by the first ScriptLink above:








From HTML code, the Taxonomy picker control needs 2 html tags:

  • One tag is a hidden tag, and will save the selected terms in the format: Label|Term_GUID;OtherLabel|TermGUID…
  • The other tag is a DIV container, where JS code will inject the control, the show picker image, autocomplete options, and so on



Now, you need to Initialize the control from more JavaScript code. As you likely will need the Taxonomy JS code and a SharePoint context, this is a good way to ensure all the JS files needed are loaded first:






Then, in the Init function, let’s initialize the Taxonomy picker control:
















































































Most of the params are self-explained, and also are the same params in the TaxonomyWebTaggingControl class. Interesting params are:

  • taxControlContentId: this is the ID from the DIV container in the HTML I showed you above.
  • InputFieldId: ID of the Hidden control.
  • SspId: This is the TermStore ID. I’ll show you later how to get that ID from more JS code.
  • termSetId: Term set ID that you want to bind the control. You can get it using sp.taxonomy.js, for example, get the ID from the TermSet name.

Here is the code to get the Term Store ID (SspId):


































Finally, let’s say you wanna set a value before loading the control, for example, you get the Field value or a User profile Taxonomy property, and you want to set the control with that value. In this case, you just have to set the Hidden control value, like:


You have to do that before configuring the control, before calling the:


and that’s all ! you will get all the Taxonomy picker functionality and styling (obviously, you have to manage what you wanna do with the selected values, the loading and saving functionality is not done by the control)

Last note

If you’re using SharePoint apps and want to use the Taxonomy picker, you can use the code from AMS Office code, where Richard diZerega has made a great job creating a JS control with almost same functionality that the OOB Taxonomy picker control.

Also, that code can be used inside SharePoint, as is using just jQuery and SP JSOM. I did it and worked fine. However, that control has some lacks comparing with the OOB, like for example, you can’t use the keyboard to select the Terms, only the mouse (users are use to type, use the arrows, and enter tab to select a Term). This can’t be done with the code sample (unless I did some wront, didn’t worked that way to me). Of course, this is some sample code, and you have the entire source to adapt it to your needs.

Hope it helps!

Luis Manez
@luismanez

Adding Windows Azure Active Directory to an existing ASP.NET app

If you want to secure your Azure ASP.NET app using Azure Active Directory, Visual Studio 2013 does a great job if you create a new one. Here you can find a good article about how to do it.

However, is not so easy and we don’t have too much help if we want to add Azure AD to an existing asp.net app. This post tries to help with that, basically, create a new project configured with Azure AD, and then follow the following steps using the new one as reference:

First you’ll need to add these packages to your project:


Copy the Utils folder that contains the class DatabaseIssuerNameRegistry and the Models classes (see image below):

image

Note: You’ll need an SQL Database for the Authentication stuff. It seems that in recent versions, the identity providers are storing some info about tokens there. In this article from great Vittorio Bertocci there are more information: http://www.cloudidentity.com/blog/2013/10/24/vs2013-rtm-organizational-accounts-and-publishing-to-windows-azure-web-sites/

Copy IdentityConfig.cs to App_Start

clip_image001

Copy AccountController

clip_image002

In the global.asax file:

Copy the method: WSFederationAuthenticationModule_RedirectingToIdentityProvider

In the Application_Start() add:

clip_image003

Copy the Account Views

clip_image004

Copy the Home/UserProfile view

clip_image005

Update the Home controller with the UserProfile action. You will see that you need to add some constants that are defined in the HomeController class


Copy LoginPartial view

clip_image001[4]

Add this code to _Layout.cshtml file

clip_image002[4]

OK. Now you need to configure some bits in the Azure portal. Basically, what you need to do is adding permissions for your app in the Azure AD.

In Azure Management Portal, go to the Active Directory and Applications, and Add a new Application:

clip_image001[6]

image

image

The sign on URL is where your app will go when you navigate to your app without being logged in. Usually will be you same home page (has to be SSL)

The App ID URI has to be your Azure Active Directory URL, followed for some app name or ID. You can see your Azure AD URL managing your Azure AD in the Domains section. If you are in the Azure tenant for Office 365, the AD URL will be the same Office 365 main URL: https://tenant_name.sharepoint.com

Then, in the Configure page, you have to add a Key:

clip_image001[8]

Save the config and you’ll see the Key value:

image

You also need to copy the Client ID

image

If you want to allow Read/Write Azure AD data, you need to configure permissions. If you have copy the HomeController as I told you before, the UserProfile action is trying to read the Active Directory, so, you need to give it permissions or you will get an error.

clip_image001[10]

We’re done with the Azure management portal, now, grab the Client ID and Key value, and come back to the asp.net project, let’s update the web.config file with all the Identity providers and config.

In ConfigSections:


Add the ConnectionString. Here you can set a local DB value, and later, when you publish to Azure, Visual Studio will detect that you’re using a connection string, and will show you the assistant to connect to the SQL DB in Azure.


Note: If you try to connect to Azure SQL DB from Visual Studio, you probably will need to configure the Allowed IP Addresses. You have more info in this article: http://azure.microsoft.com/en-us/documentation/articles/web-sites-dotnet-deploy-aspnet-mvc-app-membership-oauth-sql-database/

Add these AppSettings, review all the values that comes from the config you’ve done in Azure portal:


Add:


Adding in System.Web

clip_image001[12]

Add (again, review all the values)


And that’s all !! remember to take care of all the URLs and IDs, but if you publish to Azure, you should get the Azure AD login page.

Extra ball !!

OK. You’ve done that, however, when you go to your home page, you find a redirect to the “old style” Forms authentication Login.aspx page… WTH !!

No panic, you’re not gonna believe the reason, but ensure you don’t have the WebMatrix.Data.dll in your BIN folder… yes! it’s true

image

I found the solution in this post:

http://blogs.catapultsystems.com/rhutton/archive/2012/07/20/mvc3-with-windows-authentication-redirected-to-accountlogin.aspx

Take care because you can remove the DLL from your project, but if it’s already in your Azure website, I suggest that you browse the BIN folder in the Azure site using Visual Studio Server explorer to ensure the DLL is not there (remove it manually if needed).

Hope it helps!!

Luis Manez

@luismanez

[Office 365] Selecting webpart title from Display Template

Let’s say we have a Search Results webpart showing latest 5 news with a wonderful Webpart title saying “News”. It would be pretty interesting that we can click on the webpart title and jump to a custom page that shows all the News article.

OK, SharePoint does a great job with Display templates, where you can control the chrome of all the results and each item, however the webpart title is not rendered directly by the Display template engine, so, if we want to modify the Webpart Title we’d need some jQuery magic from the Control display template.

image

We can use our custom Control Display template, and inside, we can select the Webpart title using jQuery. The selector we need looks like that:


That way, adding more jQuery we can replace the titleElement with all the html needed, like a link to the News page.

Hope it helps!

Luis Manez

@luismanez

European SharePoint Conference – I was there (and took some notes)

image

Just arrived from the European SharePoint Conference 14 in Barcelona. This has been my first SP European event and if I have to summarized in one word, would be “amazing”. First I’ve been able to chat again with some of the Spanish SharePointers that I din´t see for a while, and second, cos I could meet in person, some of the European SP rock stars!

The organisation has been quite good, the quality of the speakers, really good, and the sessions quite good, although we expected more in some sessions, that didn’t´t include the last updates in their demos.

About the content, no new secrets, more on Office 365, more on Yammer, and more Apps, apps, apps. I´ve been pretty impressive that most of the demos were done using iPads, just highlighting the importance of the multi-device and mobile first for Microsoft.

Most of the speakers have available their presentations in their blogs, so, you can take a look to the sessions in the conference site, and look for their blogs.

I also found interesting that Microsoft is listening us. If you wanna say something about Office 365, this is the place: http://officespdev.uservoice.com/

Finally, just say thanks to my company, Content and Code, for let me attend the event, and last mention for my fellow Chris O’Brien, that was included, again, between the Top 25 European SharePoint Influencers. Congrats mate!!

Cheers.

[Office 365] Enable / Disable Feature from Power Shell using CSOM

Here we have 2 PowerShell scripts to Enable-Disable a feature using CSOM

Enable Feature

  • User: user in the Office 365 tenant
  • Password: user password
  • Url: Full URL to the site or web
  • Scope: Site / Web. Scope of the feature that we want to activate.
  • Feature: Feature Id.
  • Force: Re-enable feature if is already activated
  • Sandbox: true/false. Basically this param indicates if the solution where the feature lives, is a Farm solution, or a Sandbox solution.

About the Sandbox param. This is a little confuse if we take a look to the MSDN documentation

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.client.featurecollection.add.aspx

If we look at the FeatureDefinitionScope enum, we have:


However, the documentation in the Add method says that you can only use 2 values: Farm and Site. This param can make you be wrong, because you can think that is the Scope of the feature, so, if you want to activate a Web scope feature, you’d try with the Web value in the enum type. However, this param indicates the scope of the Solution that contains the feature: Farm or Sandbox solution, so, the possible values are only Farm and Site.

Microsoft, Why are you using an Enum with 4 values in a function that only accept 2 possible values from the Enum ??? OK… that’s another question…

Let’s see now how we can use the same approach to Disable a Feature.

Disable Feature