Powered By Blogger

Friday, September 23, 2011

Custom Page for Upload in Document Library

Now our requirement is to create a upload page where "Add as a new version to existing files" option will be unchecked by default. We don't want to change the upload.aspx file of SharePoint, hence we will create a new custom upload page. To use that custom page, we have to modify the List Template of Document Library. Again we keep away from modifying the original List Template, we need to have our custom List template for document Library. 


Here is how we approach:
1.Create a Feature which will install the custom Document Library Template.
2.Create a new control template which renders the New menu, Upload menu, Actions menu and Settings menu in the toolbar
3.To customize the Upload Menu, Create a class which inherits from the Microsoft. SharePoint.WebControls.UploadMenu class and Override the “SetmenuItemProperties” method to navigate to the custom upload page.
4.Create the custom upload page and make changes in it to suit your purpose.


Technical Details:


1. Feature: Copy and rename SharePoint feature for document library and make the only change in schema.xml
View BaseViewID="1" Type="HTML" WebPartZoneID="Main" ToolbarTemplate="MyDocumentLibraryViewToolBar"

2. Create a new control template with the following rendering template.

<SharePoint:RenderingTemplate ID="MyDocumentLibraryViewToolBar" runat="server">
<Template>
<wssuc:ToolBar CssClass="ms-menutoolbar" EnableViewState="false" id="toolBarTbl" ButtonSeparator="<img src='/_layouts/images/blank.gif' alt=''>" RightButtonSeparator="&nbsp;&nbsp;" runat="server">
<Template_Buttons>
<SharePoint:NewMenu ID="NewMenu1" AccessKey="<%$Resources:wss,tb_NewMenu_AK%>" runat="server"/>
<CustomUpload:DocLibUploadMenu ID="UploadMenu1" AccessKey="<%$Resources:wss,tb_UploadMenu_AK%>" runat="server"/>
<SharePoint:ActionsMenu ID="ActionsMenu1" AccessKey="<%$Resources:wss,tb_ActionsMenu_AK%>" runat="server"/>
<SharePoint:SettingsMenu ID="SettingsMenu1" AccessKey="<%$Resources:wss,tb_SettingsMenu_AK%>" runat="server"/>
</Template_Buttons>
<Template_RightButtons>
 <SharePoint:PagingButton runat="server"/>
 <SharePoint:ListViewSelector runat="server"/>
</Template_RightButtons>
</wssuc:ToolBar>
</Template>
</SharePoint:RenderingTemplate>



Add Assembly Name and Register Tag prefix of the class we are going to create.

<%@Assembly Name ="MyCustomDocLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddad7f64566213ec" %>


<%@Register TagPrefix="CustomUpload" Assembly ="MyCustomDocLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=ddad7f64566213ec" Namespace="MyCustomDocLib" %>



3. Create a Class File to customize the Upload Menu function.(as refered above CustomUpload:DocLibUploadMenu)



using Microsoft.SharePoint.WebControls;


namespace MyCustomDocLib
{
  public class DocLibUploadMenu : UploadMenu 
  {
    protected override void SetMenuItemProperties()
    {

if (!DoclibExplorerView)
          {
            SPListTemplateType type2;
            if (base.RenderContext.List.BaseTemplate == SPListTemplateType.WebPageLibrary)
            {
              this.Visible = false;
              return;
            }
            base.MenuTemplateControl.LargeIconMode = true;
            base.MenuControl.MenuFormat = MenuFormat.ArrowSplitButton;
            base.MenuControl.HoverCellInActiveCssClass = "ms-splitbutton";
            base.MenuControl.HoverCellActiveCssClass = "ms-splitbuttonhover";
            string str2 = site.ServerRelativeUrl + "%2F" + SPHttpUtility.UrlKeyValueEncode(base.RenderContext.List.RootFolder.ToString());
            string prefixedUrl = WebLayoutsPath + "customupload.aspx?List=" + SPHttpUtility.HtmlEncode(this.List.ID.ToString("B").ToUpper(CultureInfo.InvariantCulture) + "&RootFolder=" + str2 + "&Source=" + SPHttpUtility.HtmlEncode(this.Web.Site.MakeFullUrl(SPUtility.OriginalServerRelativeRequestUrl))) ;//+ "&Myquery=" + item["CustomId"].ToString());
            //string prefixedUrl = WebLayoutsPath + "Upload.aspx?List=1234&RootFolder=me&Source=something&MyQuery=its custom";
            base.MenuControl.ClientOnClickScript = "STSNavigate('" + SPHttpUtility.EcmaScriptStringLiteralEncode(SPUtility.GetServerRelativeUrlFromPrefixedUrl(prefixedUrl)) + "')";
}
}
}




4.Copy upload.aspx and rename it to customupload.aspx and make changes to suit your purpose.



Get the full code here:
CustomDocLibCode

Sorting Dropdown list in ASP.NET using C#

Okay, Break from SharePoint.
This is something that we often come across in coding.Got this from another developer.
Keeping this here for future reference.
http://www.codeproject.com/KB/aspnet/ASPNET_DropDownList_Sort.aspx

Sorting Dropdown list in ASP.NET using C#

Introduction

I found a big problem in ASP.NET is there was no direct method for sorting the items in the Dropdown list. Sometimes this sorting functionality is required in the applications.
Here I gave a small and easy technique of sorting the Dropdown list in ASP.NET

Using the code

I assumed that, you have a Dropdown list in your ASPX page with name "DropDownList1". I have added few items to this Dropdown list in the Page_Load event with disorder manner.
    protected void Page_Load(object sender, EventArgs e)
    {
        this.DropDownList1.Items.Add("Orange");
        this.DropDownList1.Items.Add("Grapes");
        this.DropDownList1.Items.Add("Apple");
        this.DropDownList1.Items.Add("Mango");
        this.DropDownList1.Items.Add("Lemon");
        this.DropDownList1.Items.Add("Banana");
        
    SortDDL(ref this.DropDownList1);
    } 
In the Page_Load event, I have called a function "SortDDL" for sorting the Dropdown list. In this function we have to pass the reference of the required Dropdown list. Then system will automatically sort the items in the given Dropdown list.
     private void SortDDL(ref DropDownList objDDL)
     {
        ArrayList textList = new ArrayList();
        ArrayList valueList = new ArrayList();


        foreach (ListItem li in objDDL.Items)
        {
            textList.Add(li.Text);
        }    

        textList.Sort();


        foreach (object item in textList)
        {
            string value = objDDL.Items.FindByText(item.ToString()).Value;
            valueList.Add(value);
        }
        objDDL.Items.Clear();

    for(int i = 0; i < textList.Count; i++)
        {
            ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
            objDDL.Items.Add(objItem);
        }
     }
We should need to import the following namespace for getting ArrayList class.
#using System.Collections;