Pages

Search This Blog

Monday, December 5, 2011

Creating custom fly out menu : SharePoint 2010

Many a times, we need to add navigational elements in the default sharepoint navigation to allow the users to browse to our custom site pages/application pages. One good place for this is the sharepoint 'Site Actions' menu.

We can add custom fly out menus in the 'Site Actions' menu to allow the users to browse to our pages.

In order to do this, we need to create a class inheriting from the 'Control' class in the 'System.Web.UI.Controls' namespace.

In this class, we override the method 'CreateChildControls' and make use of the classes 'SubMenuTemplate' and 'MenuItemTemplate'in the 'Microsoft.SharePoint.WebControls' namespace to create menu items.

The code for the control is as follows:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using System.Web.UI;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace Blog_Test_Project.CustomControls
{
    public class CustomMenuControl:Control
    {
        string SubMenuIconUrl = @"/_layouts/images/menunewitem.gif";
        string MenuIconUrl = @"/_layouts/images/menunewitem.gif";
        string AppPage1Url = @"/_layouts/Blog_Test_Project/PostOnFacebook.aspx";
        string AppPage2Url = @"/_layouts/Blog_Test_Project/TypeAheadSearch.aspx";

        protected override void CreateChildControls()
        {
            SPWeb site = SPContext.Current.Web;
            // create fly out menu
            SubMenuTemplate smt = new SubMenuTemplate();
            smt.ID = "CustomSubMenu";
            smt.Text = "Test Project Menu Control";
            smt.Description = "Test Custom fly out menu";
            smt.MenuGroupId = 1;
            smt.Sequence = 1;
            smt.ImageUrl = site.Url + SubMenuIconUrl;
            // create fly out menu command 1
            MenuItemTemplate mit1 = new MenuItemTemplate();
            mit1.ID = "CustomFlyoutMenu1";
            mit1.Text = "Post on your facebook wall";
            mit1.Description = "This page is used to post on your facebook wall from a sharepoint page.";
            mit1.Sequence = 1;
            mit1.ClientOnClickNavigateUrl = site.Url + AppPage1Url;
            mit1.ImageUrl = site.Url + MenuIconUrl;
            // create fly out menu command 2
            MenuItemTemplate mit2 = new MenuItemTemplate();
            mit2.ID = "CustomFlyoutMenu2";
            mit2.Text = "Check out type ahead search in a document library";
            mit2.Description = "This page is used to do type ahead search in a document library.";
            mit2.Sequence = 2;
            mit2.ClientOnClickNavigateUrl = site.Url + AppPage2Url;
            mit2.ImageUrl = site.Url + MenuIconUrl;
            // add menu commands to Controls collection of fly out menu
            smt.Controls.Add(mit1);
            smt.Controls.Add(mit2);
            // add fly out menu to Controls collection of of menu control
            this.Controls.Add(smt);
        }
    }
}


Once this is done , we add an item of type 'Empty Element' in our sharepoint project:


and then we add the following 'CustomAction' element in the elements.xml file:



<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction Id="TestCustomMenuControl"
GroupId="SiteActions"
Location="Microsoft.SharePoint.StandardMenu"
Sequence="1"
ControlAssembly="$SharePoint.Project.AssemblyFullName$"
ControlClass="Blog_Test_Project.CustomControls.CustomMenuControl"
Title="Custom Menu Control Test"
Description="Custom Menu Control Test"
/> 
</Elements>


After adding the element, we also need to add a safecontrol entry for this class since it is a custom control.We can do this by clicking on the Element in Visual Studio, clicking on 'SafeControls' in its property sheet and clicking on 'Add' to create a new entry:




Once this is done, we can deploy the solution by clicking on 'Deploy' for the sharepoint project in Visual Studio.



Once the solution is deployed, we can see the results by going to the site and opening up the 'Site Actions' menu.



Hope this helps!!

Saturday, December 3, 2011

Uncustomized the PageLayout or Page via SharePoint API


Sometimes we have a requirement where we need to remove all customization from the PageLayout or from the Page. OOB SharePoint provide link to reset to site definition. But is someone needs to do that via code then below code will help them.

SPSecurity.RunWithElevatedPrivileges(delegate()
                      {
                          using (SPSite site = new SPSite("http://sharepoint2010"))
                          {
                              PublishingSite publishingSite = new PublishingSite(site);
                              PageLayoutCollection pageCollection = publishingSite.PageLayouts;
                              foreach (PageLayout layout in pageCollection)
                              {
                                  SPFile currentFile = site.RootWeb.GetFile(layout.ServerRelativeUrl);
                                  if (currentFile.CustomizedPageStatus == SPCustomizedPageStatus.Customized || currentFile.CustomizedPageStatus == SPCustomizedPageStatus.None)
                                  {
                                      try
                                      {
                                          currentFile.RevertContentStream();
                                          currentFile.Versions.DeleteAll();
                                          currentFile.Update();
                                          site.RootWeb.Update();
                                      }
                                      catch (Exception ex)
                                      {
                                          Console.WriteLine("Error occuured : " + ex.Message);
                                      }

                                  }
                              }

                          }
                      });

Hope it will helps!!! 

Thursday, December 1, 2011

Associating File Type with SharePoint Parser

Earlier I have posted the post showing the parsers that are available in the SharePoint. In this post I will show how to associate a file type with the SharePoint default parsers. This can be helpful if anyone wants to parse the file with its own extension but want to use the default parser. In this I have created a document parser object and added that object to the Pluggable Parser set available to the Web Service.

CODE SIPPET


namespace ParserExample
{
    class Program
    {
        static void Main(string[] args)
        {
            #region Custom Extension Association

            //getting the Plugable Parsers
            SPWebService objService = SPWebService.ContentService;
            Dictionary<string, SPDocumentParser> parser =
objService.PluggableParsers;

            //Creating a custom parser
            string extension = "txt";
            string progID = parser["docx"].ProgId;
            SPDocumentParser customParser = new SPDocumentParser(progID,
extension);

            //Removing the parser if already exists
            if (parser.ContainsKey(extension))
            {
                parser.Remove(extension);
                objService.Update();
            }

            //Adding the custom parser to the pluggable parser collection
            objService.PluggableParsers.Add(extension, customParser);
            objService.Update();

            #endregion
        }
    }
}


Output

Solution to error: "There is a problem saving the file" while opening a Word template in sharepoint 2010.

Yesterday I ran into an issue while creating a document from template in Sharepoint. Instead of the document being opened in MS word 2010, I received the following error:

"There is a problem saving the file.
 Usually this is because the disk or floppy disk is too small for the file or is full, RAM memory is low, or there is a permission problem with the drive the file is being saved to.
 If the amount of disk space for a paging file is low, save the file to another drive. If the RAM memory is low, increase available RAM. If permissions to the drive do not allow you to save to that drive, save the file to another drive or request permissions from the administrator to save files to the drive.
 Note   This error can also occur if the computer is running a version of antivirus software that is incompatible with the Microsoft Office or must be updated."

Although tried many solutions but none worked for me. Finally I was able to resolve the issue by making changes to MS word 2010. After some investigation I found that it occurs due to conflict between Word preview handler and Word application.


Open Microsoft word 2010

Navigate to File -> Options -> Trust Centre



Select Trust centre settings in the right side



Select "Protected view" in the left pane



Disable all the protected view options.