Friday, November 9, 2012

Sharepoint 2013 - Add Geolocation Field in the display view of a discussion List

Imagine the scenario : You 'are on a community, you want to be able to post your position on a post to show the other users your localization. (FB like).

The idea WOULD be :
  • Add a geolocation field
  • Add the field to the display
  • TADAAM it works...
Well obviously, it doesn't!

The method is a bit more complicated. We've worked on it with Laurent Fauret (also from Sogeti) who made the js updates :

Step 1 : Create a site Column Geolocation field
The geolocation fields can't be created manually. So you have to create them using A powershell script or Using Create Geolocation field projet on Code.msdn.microsoft. Add a field in yor discussion list.

Step 2 : Update the discussion display file
The problem is that you never open the viewItems.aspx form when you navigate inside a discussion, you are always on a custom view, so the field you have added on your list, will never show up! So, it means that you have to modify the display js file!

This is not a good way of doing but it's a fast one! You should never touch the standard file of sharepoint, but instead create your owns specific but in this case we will do it for demo purpose (the good way would be to create a discussion list type based on the new display js file!)

So you have to modify the sp.ui.discussions.js located in the 15/TEMPLATE/Layouts. It manages the display of the discussion list. (use the sp.ui.discussions.debug.js to have the indented version).

  • Add your field in the viewfields list in the function
     SP.UI.Discussions.Helpers.$1f = function (m, i) 
  • Change the $2u_0: function (a, d)  to
     $2u_0: function (a, d) {
        ULS06I:;
        this.populateFolderMetalineItems(d);
        a.addCssClass("ms-comm-metalineList ms-noList");

        a.renderBeginTag("ul");
        if (this.jsonItem["My_x0020_Location"]) {
            a.addCssClass("ms-comm-metalineItemSeparator");
            a.addCssClass("ms-comm-metalineItem");
            a.renderBeginTag("li");
            a.write(spMgr.RenderFieldByName(this.$0_0, "My_x0020_Location", this.jsonItem, this.$0_0.ListSchema))
            a.renderEndTag()
        }
        for (var b = 0, c = this.$R_0.length; b < c; b++) {
            b !== c - 1 && a.addCssClass("ms-comm-metalineItemSeparator");
            a.addCssClass("ms-comm-metalineItem");
            a.renderBeginTag("li");
            a.write(this.$R_0[b]);
            a.renderEndTag()
        }
        a.renderEndTag()

    },

  • Change the $2y_0: function (a) { to$2y_0: function (a) {
            ULS06I:;
            var loc = spMgr.RenderFieldByName(this.$0_0, "My_x0020_Location", this.jsonItem, this.$0_0.ListSchema);
            if (loc) {
                a.addCssClass("ms-metadata");
                a.addCssClass("ms-comm-cmdSpaceListItem");
                a.renderBeginTag("span");
                a.write(loc);
                a.renderEndTag();
            }
        a.addCssClass("ms-metadata");
        a.addCssClass("ms-comm-cmdSpaceListItem");
        a.renderBeginTag("span");
        var b = spMgr.RenderFieldByName(this.$0_0, "Created", this.jsonItem, this.$0_0.ListSchema);
        a.write(b);
        a.renderEndTag()
    },



Note :We have called our column "My Location".



Step 3 : Test
It works only on your /discussion%20List/forms/allitems.aspx ... but it f you want to see it working on your site homepage (where the discussion list is already put), you have to revalidate the "subject" view on the discussion list Webpart for the view to be updated with your new field.
An now you will see your GeoLoc picto on your discussion!


And on the click



5 comments:

  1. Hello,
    I replaced default sp.ui.discussions.debug.js script in JSLink with custom one for the View. But it is not working because default script loads into the page. Have no idea how it is getting there. Have JSLink field = sp.ui.communities.js|custom.sp.ui.discussions.debug.js

    Did you have this issue?

    ReplyDelete
    Replies
    1. Hello, if the old one still loads, it should only be because you should do a iisreset to make it work!
      Open you firefox's firebug (oupppps, no, sorry i meant your IE), and see if it's really the good HTML that is loaded.
      Good luck with that!

      Delete
  2. sp.ui.discussions.debug.js is linked to DiscussionListTemplateID (108), so if you add another built-in discussion standard WebPart, the last loaded sp.ui.discussion.js will override the Client side rendering process of the whole page. You may need to build a new List Template ID, and in the GlobalOverrideContext method the target ListTemplateID.

    ReplyDelete
  3. how to create a discussion list type based on the new display js file?

    ReplyDelete
  4. Hi,
    I'm still fresh for sharepoint. I create sp.ui.discussions.js files in my project folder and replace it AllItems.aspx file's /SiteAssets/js/sp.ui.discussions.js . But it does not work. what should I do for that issue?

    Thank You

    ReplyDelete