Showing posts with label FarmSolution. Show all posts
Showing posts with label FarmSolution. Show all posts

Monday, September 11, 2017

Deploying SharePoint 2016 master pages using visual studio farm solution

In this post, I will be sharing my recent experience where I had been working on creating SharePoint 2016 HTML master page for intranet. Initially for my development purpose I was directly creating HTML master page using SharePoint designer but when the time came, I was asked to deploy to different environment such as test environment. I finally ended up creating farm based visual studio code solution. Below are steps I had used for creating the wsp solution.

1. Here is the  final structure of solution.



2. Start your visual studio and select empty SharePoint project with farm based solution approach.

3. Add folder called 'FeatureElements' to the project.

4. Right click on FeatureElements and then add module to the folder as shown below.


5. Now, right under the module start adding your HTML files. In my case, I had all my master pages created using SharePoint designer based on seattle.html and I had imported those HTML files into visual studio solution.

6.In the element.xml under the module, make sure to the entries are present for HTML files.


7. If there are any resource files such as CSS/JavaScript/Images you can add those files to this solution by deploying it to Styles/1033 folder or layout folder of 16 hive as shown in above folder structure.

8. Now, at this stage if we deploy the above solution, html master pages will be available in master page gallery of the site collection and here you need to publish the html master page by using context menu and then it generates .master file. At this stage  you apply the master page to the site. Entire process can be automated by creating a feature receiver as shown below.

9.Now, Right click on feature receiver to add event receiver as shown in the image below.



10. Once the file is created, In feature activated method add the below code


11. Double click on feature receiver to see the below properties.


12. Use the below PowerShell script to deploy the wsp to the environment.

Install-SPSolution -Identity MasterPageCustomSolution.wsp -WebApplication https://abc.com -FullTrustBinDeployment -force

13. Once the solution is deployment, go to site collection feature and activate the feature. The master page will be available for you in master page settings page and can set it as default master page.

Monday, June 20, 2016

JavaScript in Application Pages - Farm Solution Deployment

Below examples shows deploying an application page that contains JavaScript using Farm solution deployment approach.

I am creating an empty SharePoint project in Visual Studio. Next I had added application page with Name 'JSDemo.aspx'. Below is the folder structure for the project.

 

 In Application page, I had created input button with click action, Essentially when I click the button I am loading all the lists present in the site that has an item count greater than 0.

I had used sp.js to create context for this specific site that is declared in ScriptLink tag in the application page. Below is the complete code.

[code]
<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">
    <SharePoint:ScriptLink Name="sp.js" LoadAfterUI="true" runat="server" Localizable="false" ></SharePoint:ScriptLink>

    <%--load intellisense for sp.js--%>
    <% #if ZZZZ %>
    <script type="text/javascript" src="/_layouts/SP.debug.js"></script>
    <% #endif %>

    <script type="text/javascript">
        var lists;
        function getLists() {
            var context = SP.ClientContext.get_current();
            var web = context.get_web();
            context.load(web, "Title", "Description");
            lists = web.get_lists();
            context.load(lists, "Include(Title,ItemCount)");
            context.executeQueryAsync(onSuccess,onFail);
        }

        function onSuccess() {
            var ul = document.getElementById("listitems");
            var listsEnum = lists.getEnumerator();
            while (listsEnum.moveNext()) {
                var list = listsEnum.get_current();
                if (list.get_itemCount() > 0) {
                    var title = list.get_title();
                    var elem = document.createElement("li");
                    elem.innerText = title;
                    ul.appendChild(elem);
                }
            }
        }

        function onFail() {
            alert("Request Failed");
        }
    </script>
</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<input id="getListsButton" type="button" value="Get List items" onclick="getLists()" />
<br />
<br />
<ul id="listitems"></ul>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
Application Page
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
My Application Page
</asp:Content>
[/code]

Result: For accessing the application page I am navigating to /_layout/JSApplicationPage/JSDemo.aspx page