Introduction

This blog is mainly focused on SharePoint
I will use this to store and share all the tips and tricks about SharePoint and the related products
List of all posts

Context Menu in METRO Application


Topic Context Menu in METRO Application
OutputA context Menu

Step Description
1 Create the tags in your hmtl code
tz01 : Where the contextual menu will be show on click tz02 : Where the result of the contextual action will be show
<span id="tz01">Click Me</span><br /><span id="tz02">Result here</span>
2 in your js file create the function to know the relative location of the click
    function pageXY(pageX, pageY) {
        var zoomFactor = document.documentElement.msContentZoomFactor;
        return {
            x: (pageX - window.pageXOffset) * zoomFactor,
            y: (pageY - window.pageYOffset) * zoomFactor
        };
    }
3 in your js file create the function to display the result of the selected Contextual Action
    function showResult(Id) {
        document.getElementById("tz02").innerHTML = document.getElementById(Id).id + " " + "done";
    }
4 in your js file create the function to display the Contextual Menu
     function contextMenu() {
        var menu = new Windows.UI.Popups.PopupMenu();
        menu.commands.append(new Windows.UI.Popups.UICommand("Action 1" , showResult(window.event.srcElement.getAttribute("Id")), 1));
        menu.commands.append(new Windows.UI.Popups.UICommandSeparator);
        menu.commands.append(new Windows.UI.Popups.UICommand("Action 2", null, 2));
        menu.commands.append(new Windows.UI.Popups.UICommand("Action 3", null, 3));
        menu.showAsync(pageXY(window.event.clientX, window.event.clientY));
    }
5 in your js file create the function to attach the eventlistener to the tag
function Attachcm() {
    document.getElementById("tz01").addEventListener("click", contextMenu, false);
}
6 in your js file call your Attachcm function in the ready function of your page
(function () {
    "use strict";
    WinJS.UI.Pages.define("/pages/mypage.html", {
        // This function is called whenever a user navigates to this page. It
        // populates the page elements with the app's data.
        ready: function (element, options) {
            // TODO: Initialize the page here.
            Attachcm();
        },
        unload: function () {
            // TODO: Respond to navigations away from this page.
        },

        updateLayout: function (element, viewState, lastViewState) {
            /// 

            // TODO: Respond to changes in viewState.
        }
    });
})();
Preview :

Building and saving JSON Object to file in METRO Application


Topic Building and saving JSON Object to file in METRO Application
OutputThe dedicated functions

Step Description
1 Create the jason object
function TestJson() {
    Windows.Storage.ApplicationData.current.localFolder.createFileAsync("sample.json", Windows.Storage.CreationCollisionOption.openIfExists).done(
        Windows.Storage.ApplicationData.current.localFolder.getFileAsync("sample.json").done(
            function (fjson) {
                var oJSON = {};
                oJSON.oTraining = {};
                oJSON.oTraining.Id = "1";
                for (var i = 0; i < 6; i++) {
                    oJSON.Sessions[i] = {};
                    oJSON.Sessions[i].Id = i;
                }
                // sub arrays
                oJSON.Sessions[5] = {};
                oJSON.Sessions[5].Id = i;
                oJSON.Sessions[5].oFract = new Array();
                oJSON.Sessions[5].oFract[0] ={};
                oJSON.Sessions[5].oFract[0].Id = 1;
                oJSON.Sessions[5].oFract[1] ={};
                oJSON.Sessions[5].oFract[1].Id = 2;
                oJSON.Sessions[5].oFract[2] ={};
                oJSON.Sessions[5].oFract[2].Id = 3;
                Windows.Storage.FileIO.writeTextAsync(fjson, JSON.stringify(oJSON));
            }
        )
    )
}
 

Save / Load JSON Object from file in METRO Application


Topic Save / Load JSON Object from file in METRO Application
OutputThe dedicated functions

Step Description
1 Saving the JSON file
In this sample we are saving the basic info of a user
function RegisterUser() {
    Windows.Storage.ApplicationData.current.localFolder.createFileAsync("User.json", Windows.Storage.CreationCollisionOption.openIfExists).done(
        Windows.Storage.ApplicationData.current.localFolder.getFileAsync("User.json").done(
            function (fjson){
                var TabSexe = document.getElementsByName("sex");
                var sexe;
                for (var i = 0; i < TabSexe.length; i++) {
                    if (TabSexe[i].checked) {
                        sexe = TabSexe[i].value;
                    }
                }
                var UserJSON = { "Pseudo": document.getElementById("txtPseudo").value, "BirthDate": document.getElementById("ddlDateNaissance").value, "Sexe": sexe };
                Windows.Storage.FileIO.writeTextAsync(fjson, JSON.stringify(UserJSON));
            }
        )
    )
}
 
2 Loading the JSON file
In this example, we load the JSON object from a file and navigate in it.
function showSessions() {
    Windows.Storage.ApplicationData.current.localFolder.getFileAsync("Sessions.json").done(
            function (getSessions) {
                Windows.Storage.FileIO.readTextAsync(getSessions).done(
                    function (jSessions) {
                        var SessionJSON = eval("(" + jSessions + ")");
                        var ttd = document.getElementById("placer_one");
                        ttd.innerHTML = SessionJSON.Training.Type + " : " + SessionJSON.Training.Id + " : " + SessionJSON.Training.Target;
                        var ttable = document.getElementById("EntTable");
                        for (var i = 0; i < SessionJSON.Sessions.length; i++){
                            tr = document.createElement("tr");
                           // {"Id": "1", "Distance" : "1000m", "TargetTime" : "12m","RealTime" : null,"ResultType" : null,"Eval" : null},
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Id;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Distance;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].TargetTime;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].RealTime;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].ResultType;
                            tr.appendChild(td);
                            td = document.createElement("td");
                            td.innerHTML = SessionJSON.Sessions[i].Eval;
                            tr.appendChild(td);
                            ttable.appendChild(tr);
                        }
                    }
                )
            }
        )
}
 

Get or Set Value of an InfoPath xml node


Topic Get or Set Value of an InfoPath xml node
OutputThe dedicated functions

Step Description
1 Create the GetVal and SetVal functions
         /// <summary>
        /// Used to retreive the value of an xml node
        /// </summary>
        /// <param name="xpath">xpath of the node</param>
        /// <returns>node value</returns>
        private string GetVal(string xpath)
        {
            XPathNavigator myNav = this.MainDataSource.CreateNavigator().SelectSingleNode(xpath, NamespaceManager);
            if (myNav != null)
                return myNav.Value;
            else
                return "";
        }

        /// <summary>
        /// Used to set the value of an xml node
        /// </summary>
        /// <param name="xpath">Path to the node</param>
        /// <param name="valeur">value of the node</param>
        private void SetVal(string xpath, string value)
        {
            try
            {
                XPathNavigator xpRoot = this.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager);
                if (xpRoot.MoveToAttribute("nil", "http://www.w3.org/2001/XMLSchema-instance")) xpRoot.DeleteSelf();
                this.CreateNavigator().SelectSingleNode(xpath, this.NamespaceManager).SetValue(value);
            }
            catch (Exception ex)
            {
                //Only use for the following exception
                //Exception Message: Operation is not valid due to the current state of the object
            }
        }

Dynamically changing URL of Data Connection in InfoPath 2010 (by code)


Dynamically changing URL of Data Connection in InfoPath 2010 (by code)

Step Description
1Add using System.Text.RegularExpressions; at the top section of your code
2 Create a function to attribute the new url to the dataconnexion
       /// <summary>
        /// This function is used for portability 
        /// The DataConnexion path are modified to target the current SharePoint Site
        /// </summary>
        private void setDataConnexionPath()
        {
            string basepath = this.ServerInfo.SharePointSiteUrl.AbsoluteUri.ToString().Replace("/Sites/","");
            Regex regexObj;
            foreach (DataConnection dc in this.DataConnections)
            {
                if (dc is SharePointListRWQueryConnection) { 
                    regexObj = new Regex("(.*)/sites/(.*)");
                    string cpath = ((SharePointListRWQueryConnection)dc).SiteUrl.AbsoluteUri.ToString();
                    Match m_src = regexObj.Match(cpath);
                    Match m_target = regexObj.Match(basepath);
                    ((SharePointListRWQueryConnection)dc).SiteUrl = new Uri(this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/", ""));
                }
                if (dc is SharePointListRWSubmitConnection) { 
                    regexObj = new Regex("(.*)/sites/(.*)");
                    string cpath = ((SharePointListRWSubmitConnection)dc).SiteUrl.AbsoluteUri.ToString();
                    Match m_src = regexObj.Match(cpath);
                    Match m_target = regexObj.Match(basepath);
                    ((SharePointListRWSubmitConnection)dc).SiteUrl = new Uri(this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/",""));
                }
                if (dc is WebServiceConnection) {
                    if (dc.Name.StartsWith("GetUserProfileByName"))
                    {
                        //http://clipsapps.cclulaba.net:81/sites/Computa/Request/_vti_bin/userprofileservice.asmx?WSDL
                        regexObj = new Regex("(.*)/_vti_bin/(.*)");
                        string cpath = ((WebServiceConnection)dc).ServiceUrl.AbsoluteUri.ToString();
                        Match m_src = regexObj.Match(cpath);
                        ((WebServiceConnection)dc).ServiceUrl = new Uri(cpath.Replace(m_src.Groups[1].Value, this.ServerInfo.SharePointListUrl.AbsoluteUri.Replace("/FormServerTemplates/","")));
dc.Execute();
                    }
                    if (dc.Name.StartsWith("GetAllUserCollectionFromWeb"))
                    {
                        //http://clipsapps.cclulaba.net:81/_vti_bin/usergroup.asmx?wsdl
                        regexObj = new Regex("(.*)/_vti_bin/(.*)");
                        string cpath = ((WebServiceConnection)dc).ServiceUrl.AbsoluteUri.ToString();
                        Match m_src = regexObj.Match(cpath);
                        ((WebServiceConnection)dc).ServiceUrl = new Uri(cpath.Replace(m_src.Groups[1].Value,this.ServerInfo.SharePointServerRootUrl.AbsoluteUri.ToString() ));
dc.Execute();
                     }
                }
                if (dc is FileSubmitConnection) {
                    regexObj = new Regex("(.*)/sites/(.*)");
                    string cpath = ((FileSubmitConnection)dc).FolderUrl;
                    Match m_src = regexObj.Match(cpath);
                    Match m_target = regexObj.Match(basepath);
                    ((FileSubmitConnection)dc).FolderUrl = cpath.Replace(m_src.Groups[1].Value, m_target.Groups[1].Value);
               }
            }
        }

by Category