Monday 28 October 2013

Add Comment Box in SharePoint Enterprise Wiki Pages

Hi Everyone,

Today I had a new requirement to add a Comment box for all the Wiki pages. It will be used to give feedback or comments for that Wiki page content.

In SharePoint we have a very powerful webpart for that its nothing but Note Board webpart.

I thought of adding it to the Page Layout and when ever a new page is created it will be created with the new comment box.

So below code has to added to added in the page layouts to include the Note Board webpart to the wiki pages.

<SharePointPortalControls:SocialCommentWebPart id="noteboard" ChromeType="None" runat="server" />

How To Add:

Edit the page layout in sharepoint designer and add it next to the <PublishingWebControls:RichHtmlField Tag.

After adding the the note board webpart save the page layout after saving it the Note Board Webpart will take a __Webpartid automatically.

Happy SharePointing... :)

Friday 12 July 2013

How To Extract SharePoint WSP Solution and Repack to WSP

If provided a third party SharePoint WSP solution, there may be need to edit contents of the package to conform with governance for the SharePoint farm.

To extract the contents of a WSP package, make modifications, and repackage the WSP:
1. Download and install WSP Builder or WSP Builder Extensions (http://wspbuilder.codeplex.com/) from Codeplex
2. Run the command prompt as an administrator
3. Change the current directory to the directory including WSPBuilder.exe (e.g. "cd C:\Program Files (x86)\WSPTools\WSPBuilderExtensions")
4. Run the following command from the command prompt, replace {WSPLOCATION} with the location of the WSP and the {OUTPUTLOCATION} with the folder to extract the contents
    WSPBuilder.exe -o extractwsp -filename {WSPLOCATION} -targetpath {OUTPUTLOCATION}
Eg: WSPBuilder.exe -o extractwsp -filename C:\Users\JohnJay\Desktop\Temp\Template.wsp -targetpath C:\Users\JohnJay\Desktop\Temp
5. Make any modifications neccassary to the extracted files, I had to Remove the unwanted Site Features from ONet.xml file.
6. Run the following command from the command prompt, replace {WSPLOCATION} with the location of where the WSP should be created and the {OUTPUTLOCATION} where the previously extracted contents exist
   WSPBuilder.exe -ProjectPath {OUTPUTLOCATION} -SolutionPath {OUTPUTLOCATION}  -Outputpath {WSPLOCATION}
Eg: WSPBuilder.exe -ProjectPath C:\Users\JohnJay\Desktop\Temp -SolutionPath C:\Users\JohnJay\Desktop\Temp  -Outputpath C:\Users\JohnJay\Desktop\Temp
Once the WSP is packages, it should behave as a typical SharePoint solution deployable via the STSADM command line or through PowerShell.

Tuesday 21 May 2013

OnLoad Event in PageLayouts in SharePoint 2010

Today I came across a different problem. I wanted to add JavaScript or jQuery into SharePoint Page Layout which it will run when page loaded.

I'v tried jQuery $(document).ready(function() { }); and JavaScript window.onload, but nothing seemed to work.

Finally I found out that the MS SharePoint team has given the developers a way to do this easily, using a special array called _spBodyOnLoadFunctionNames. The only thing you have to do is push your function's name to this array and it will get called in the OnLoad event of the window.

Below is the function which depicts how the _spBodyOnLoadFunctionNames function is used inside Javascript.


<script type="text/javascript">
_spBodyOnLoadFunctionNames.push("HideLink");
function HideLink(){
document.getElementById('DocName').style.visibility = "hidden";
}
</script>

The above function hides the tag with id as 'DocName' when a page loads, which in turn comes from the Page Layouts.

This solution should also work for visual webparts that needs to call a javascript function OnLoad.(Not tested though..).

Happy SharePointing.... :)

Thursday 21 March 2013

Configuring Global and Current Navigation Settings Using Sandbox Solution


Today I came across with a scenario where I had the requirement to configure the Navigation Settings (available in the Look And Feel section) so as to have different quick launch or current navigation for different subsites. It seemed to be a quite simple task where you can make use of the PublishingWeb class and set the desired properties to configure global as well as current navigation.



The above image shows the Navigation option in the Look and Feel section in site settings.

The below screenshot shows the navigation settings page from where the user can manually configure the navigation options.





Following piece of code briefly explains how this can be done

PublishingWeb pubWeb = PublishingWeb.GetPublishingWeb(currentweb);
//Include pages in the quick launch
pubWeb.Navigation.CurrentIncludePages = true;
//Include subsites in the global nav
pubWeb.Navigation.GlobalIncludeSubsites = true;

But here comes a twist when you work as a part of sandbox solution. The PublishingWeb class is a part of the Microsoft.SharePoint.Publishing assembly which is not available in sandbox. As a result, you cannot use this class to configure the navigation settings. Then how can we configure these settings in the sandbox solution.
After much exploring through reflector and sharepoint manager, finally sharepoint manager came to my rescue. I tried to do the settings through UI and observed what was happening behind the scenes to the various properties of SPWeb property bag. My findings were that there are few properties like below whose value changes when we modify the various settings in the Navigation Settings page.




__CurrentNavigationIncludeTypes :- The value of this property changes when we try to select the     various options in the Current Navigation section. The various possible options are 0, 1, 2, 3
     0 indicates that in structural navigation , nothing is selected
     1 indicates that in structural navigation, Show Subsites is checked and Show Pages is unchecked
     2 indicates that in structural navigation, Show Subsites is unchecked and Show Pages is checked
     3 indicates that in structural navigation, Show Subsites is checked and Show Pages is also checked

__GlobalNavigationIncludeTypes :- The value of this property changes when we try to select the various options in the Global Navigation section. The various possible options are 0,
     0 indicates that in structural navigation , nothing is selected
     1 indicates that in structural navigation, Show Subsites is checked and Show Pages is unchecked
     2 indicates that in structural navigation, Show Subsites is unchecked and Show Pages is checked
     3 indicates that in structural navigation, Show Subsites is checked and Show Pages is also checked

__InheritCurrentNavigation :- This property if set to true inherits the parent navigation in the child web.

__NavigationShowSiblings :- This property is a boolean value that represents whether the web should display the siblings.

__IncludeSubSitesInNavigation :- This property is common to both global navigation as well as current navigation. As the name suggests, it will include subsites in the global as well as current navigation.

__IncludePagesInNavigation :- This property is common to both global navigation as well as current navigation. As the name suggests, it will include all the pages in the global as well as current navigation.


Now, in my scenario, the global as well as current navigation was supposed to be different. Following is the piece of code

public const string InheritCurrentNavigation = "__InheritCurrentNavigation";
        public const string CurrentNavigationIncludeTypes = "__CurrentNavigationIncludeTypes";
        public const string NavigationShowSiblings = "__NavigationShowSiblings";
        public const string GlobalNavigationIncludeTypes = "__GlobalNavigationIncludeTypes";
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            try
            {
                SPWeb web = properties.Feature.Parent as SPWeb;
                if (web.IsRootWeb != true)
                {
   //Set child web not to inherit from parent web
                    if (web.GetProperty(InheritCurrentNavigation) != null)
                    {
                        web.DeleteProperty(InheritCurrentNavigation);
                        web.AddProperty(InheritCurrentNavigation, bool.FalseString);
                    }
                    else
                    {
                        web.AddProperty(InheritCurrentNavigation, bool.FalseString);
                    }
   if (web.GetProperty(NavigationShowSiblings) != null)
                    {
                        web.DeleteProperty(NavigationShowSiblings);
                        web.AddProperty(NavigationShowSiblings, bool.FalseString);
                    }
                    else
                    {
                        web.AddProperty(NavigationShowSiblings, bool.FalseString);
                    }
                   //Setting the Global navigation to show only the Subsites

                    if (web.GetProperty(GlobalNavigationIncludeTypes) != null)
                    {
                        web.DeleteProperty(GlobalNavigationIncludeTypes);
                        web.AddProperty(GlobalNavigationIncludeTypes, 1);
                    }
                    else
                    {
                        web.AddProperty(GlobalNavigationIncludeTypes, 1);
                    }

   //Setting the current navigation to show only pages
                    if (web.GetProperty(CurrentNavigationIncludeTypes) != null)
                    {
                        web.DeleteProperty(CurrentNavigationIncludeTypes);
                        web.AddProperty(CurrentNavigationIncludeTypes, 2);
                    }
                    else
                    {
                        web.AddProperty(CurrentNavigationIncludeTypes, 2);
                    }
                    web.Update();
                }
            }
            catch (Exception ex)
            {
            }
        }
With the help of above properties, you can easily configure navigation settings in a sandbox solution as well. You can explore more properties with the help of SharePoint Manager. Happy SharePointing.... :)