Thursday, April 19, 2012

Custom Paging with Data List in SharePoint

 

In this article I have explained about how to create custom paging in Datalist control.

 

I will directly jump to the code. I have created a custom Visual WebPart with this Datalist to show some images stored in a image library.

The ascx page

 

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>

<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>

<%@ Import Namespace="Microsoft.SharePoint" %>

<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ImageView_EventsPhotoUserControl.ascx.cs" Inherits="My.SP.VisualWebpart.News.ImageView_EventsPhoto.ImageView_EventsPhotoUserControl" %>

<table border="0" cellpadding="0" cellspacing="0" style="width: 100%">

    <tr class="ContentADP">

        <td align="left" style="font-size: 22px; width: 50%" class="NewsHeading_LV2">

            Events Photos

        </td>

        <td style="width: 50%; text-align: right; padding-right: 10px">

            <asp:Button ID="btnPrev" runat="server" Text="<<" OnClick="btnPrev_Click" /><asp:Label

                ID="lblCurrentPage" runat="server" Text=""></asp:Label><asp:Button ID="btnNext" runat="server"

                    Text=">>" OnClick="btnNext_Click" />

        </td>

    </tr>   

</table>

<asp:DataList ID="dlImages" runat="server" RepeatColumns="4" RepeatDirection="Horizontal"

    OnItemDataBound="dlImages_ItemDataBound">

    <ItemTemplate>

        <table id="Table1" cellpadding="0" border="1" cellspacing="1">

            <tr>

                <td width="150px" style="height: 150px">

                    <asp:Image ID="imgProject" runat="server" ImageUrl='<%# Eval("RequiredField") %>'

                        ToolTip='<%# Eval("NameOrTitle") %>' />

                </td>

        </table>

    </ItemTemplate>

</asp:DataList>

 

 

 

Next is the Code behind file

using System;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Data;

using Microsoft.SharePoint;

using Microsoft.Office.Server.Diagnostics;

 

namespace My.SP.VisualWebpart.News.ImageView_EventsPhoto

{

    public partial class ImageView_EventsPhotoUserControl : UserControl

    {

        PagedDataSource pagedData = new PagedDataSource();

        public int currentPage

        {

            get

            {

                object o = this.ViewState["_currentPage"];

                if (o == null)

                    return 0;

                else

                    return (int)o;

            }

            set

            {

                this.ViewState["_currentPage"] = value;

            }

        }

 

 

        protected void Page_Load(object sender, EventArgs e)

        {

            if (!IsPostBack)

            {

                try

                {

                    BindImages();

                }

                catch (Exception ex)

                {

                    Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in ListImage web part" + ex.Message);

                    throw new SPException("Check Image view web part");

                    Response.Write(ex.ToString());

                }

            }

        }

 

        protected void BindImages()

        {

            try

            {

                SPSecurity.RunWithElevatedPrivileges(delegate()

                {

                    SPSite elevatedSite = new SPSite(SPContext.Current.Site.Url);

                    SPWeb elevatedWeb = elevatedSite.OpenWeb("/news");

                    SPList spListDocumentLib = elevatedWeb.Lists["EventsPhoto"];

                    DataTable dtItems = spListDocumentLib.Items.GetDataTable();

                    if (dtItems != null && dtItems.Rows.Count > 0)

                    {

                        pagedData.DataSource = dtItems.DefaultView;

                        pagedData.AllowPaging = true;

                        pagedData.PageSize = 12;

                        pagedData.CurrentPageIndex = currentPage;

                        btnPrev.Enabled = (!pagedData.IsFirstPage);

                        btnNext.Enabled = (!pagedData.IsLastPage);

                        lblCurrentPage.Text = "Page: " + (currentPage + 1).ToString() + " of " + pagedData.PageCount.ToString();

                        dlImages.DataSource = pagedData;

                        dlImages.DataBind();

                    }

                });

            }

            catch (Exception ex)

            {

                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in ListImage web part" + ex.Message);

                throw new SPException("Check Image view web part");

                //throw ex;

            }

        }

 

        protected void btnPrev_Click(object sender, EventArgs e)

        {

            //Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage - 1));

            currentPage -= 1;

            BindImages();

        }

 

        protected void btnNext_Click(object sender, EventArgs e)

        {

            //Response.Redirect(Request.CurrentExecutionFilePath + "?Page=" + (CurPage + 1));

            currentPage += 1;

            BindImages();

        }

 

        protected void dlImages_ItemDataBound(object sender, DataListItemEventArgs e)

        {

            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)

            {

                string siteurl = SPContext.Current.Site.Url;

                Image imgProject = (Image)e.Item.FindControl("imgProject");

                imgProject.ImageUrl = siteurl + "/news/EventsPhoto/" + imgProject.ToolTip;

                imgProject.Style.Add("width", "200px");

                imgProject.Style.Add("height", "200px");

            }

        }

    }

}

 

 

Thanks for reading..

If you have a more elegant solution – please post a comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

The list is too large to save as a template. The size of a template cannot exceed 10485760 bytes.

You may find is problem in MOSS or WSS 3.0

Saving a list as a template is an easy way of transferring data from one place to another in SharePoint. By default, however, the maximum size of list templates in WSS 3.0 and MOSS 2007 is 10MB.

By default, Microsoft has restricted the download size of the list template as 10MB (10485760 bytes). If you are trying to save site/list template which are having more than 10MB size, you will get "The list is too large to save as a template. The size of a template cannot exceed 10485760 bytes." error.

To increase the maximum size, simply run the following stsadm command (from the C:\Program Files\Common Files\Microsoft Shared\web server extensions\12\BIN directory) replacing the propertyvalue to the new maximum size (in bytes):

stsadm -o setproperty -propertyname max-template-document-size -propertyvalue 50000000

Points to note:

  • The propertyvalue parameter is in bytes, so the example above will increase the maximum size to approx 50MB.
  • There is a hard limit of 500MB so the value must be less than 524288000 or you will get an error when saving the list as a template.
  • The above increases the maximum size of both site and list templates.

 

Thanks for reading.. please post  comment… I’ll be happy to hear.

...HaPpY CoDiNg

Partha (Aurum)

Tuesday, April 17, 2012

How to change Asp.net TreeView font

I have created all the CSS class and applied, but the tree view is not changing the font size. After sever rounds of try I minutely examined the view source and could file that the CssClass is used in specifying ASP:Treeview elements is being override by

 style="border-style:none;font-size:1em;"
this 1em is finally being added to the "<a" tags within. As a result, no font-size specifics are effective.

Finally finding no other way, I planned to change the final rendering (I have used this in a Visual web part and used in SharePoint )

using Microsoft.Office.Server.Diagnostics;

using Microsoft.SharePoint;

using System.IO;

using System.Text.RegularExpressions;

 

#region Changing the Font Size

        protected override void Render(HtmlTextWriter writer)

        {

            try

            {

                HtmlTextWriter output = new HtmlTextWriter(new StringWriter());

                base.Render(output);

                string outputString = output.InnerWriter.ToString();

                //strip out all of the inline styles that the asp menu adds

                //outputString = Regex.Replace(outputString, "style=\"*\"", string.Empty);

                    if (outputString.Contains("1em;"))

                    {

                        outputString = outputString.Replace("1em;", "10pt;");

                    }

                    //outputString = Regex.Replace(outputString,"style=\"border-style: none; font-size: 1em;\"", "style=\"border-style: none;\"");

                    writer.Write(outputString);

            }

            catch (Exception ex)

            {

                Microsoft.Office.Server.Diagnostics.PortalLog.LogString("Exception Occured in Left Menu - Control :" + ex.Message);

                throw new SPException("Check Left Menu or Left Menu String");

            }

        }

        #endregion

 

 

You can use Regex.Replace or String.Replace. If you use Regex.Replace create the regular expression likewise.

If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Ref:

http://msdn.microsoft.com/en-us/library/h4kete56.aspx

http://msdn.microsoft.com/en-us/library/ms476607.aspx

http://oreilly.com/windows/archive/csharp-regular-expressions.html

http://www.dotnetperls.com/regex-replace

http://msdn.microsoft.com/en-us/library/xwewhkd1.aspx

Monday, April 16, 2012

How to refer custom.css into SharePoint 2010 sites

This time I got to create my custom css for my SharePoint 2010 custom master page. Now, how to add/refer my custom css?

The Solution is simple, refer in the following way:

<SharePoint:CssRegistration ID="CssRegistration1" Name="/Style Library/Home/CSS/mycustom.css" runat="server" After="corev4.css" /> 

SharePoint 2010 uses corev4.css, located at /layouts/1033/styles/Themable/corev4.css on the server.

We can customize CSS just as we could on MOSS 2007, but in SharePoint 2010 we will customize it in the core4.css file. Unlike MOSS, when we customize CSS for a SharePoint 2010 site, the _Styles folder is not automatically created on the current site. When customizing CSS in SharePoint 2010, the _Styles folder is gotten from folder 14 on the server and runs from there.

Here are some of the most common ways to reference custom CSS for SharePoint 2010. One key change over 2007 is the ability to specify After=”corev4.css” in the CssRegistration to make sure your custom CSS is referenced after the OOTB corev4.css file.

<SharePoint:CssLink runat="server" Version="4"/>
Note: Default OOTB
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>

<SharePoint:Theme runat="server"/>
Note: If a theme is enabled
Output Order:

  1. <link rel=”stylesheet” type=”text/css” href=”/_themes/[UniqueCounter#]/corev4-[u=UniqueHex#].css?ctag=[UniqueCounter#]”/>

<SharePoint:CssRegistration name=”customfolder/samplecustom.css” runat=”server”/>
Note: Alternate CSS on Server but comes before corev4.css
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>

<SharePoint:CssRegistration name=”customfolder/samplecustom.css” After=”corev4.css” runat=”server”/>
Note: Alternate CSS on Server and comes after corev4.css.
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>

<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/samplecustom.css %>" After="corev4.css" runat="server"/>
Note: Alternate CSS In SharePoint Database for sites without publishing enabled
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/Style%20Library/emc_2010_custom.css"/>

<SharePoint:CssRegistration name="<% $SPUrl:~sitecollection/Style Library/~language/Themable/Core Styles/samplecustom.css %>" After="corev4.css" runat="server"/>
Note: Alternate CSS In SharePoint Database for sites with publishing enabled
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/[sitename]/Style%20Library/en-US/Themable/Core%20Styles/emc_2010_custom.css"/>

<link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css"/>
Note: Alternate CSS on Server by using the standard link rel. Does not need the After attribute…
Output Order:

  1. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/Themable/corev4.css"/>
  2. <link rel="stylesheet" type="text/css" href="/_layouts/1033/styles/customfolder/samplecustom.css" />

 

If you have a more elegant solution – please post a comment… I’ll be happy to hear.
...HaPpY CoDiNg

Partha (Aurum)

Ref:

http://erikswenson.blogspot.com/2010/01/sharepoint-2010-base-css-classes.html

http://community.bamboosolutions.com/blogs/sharepoint-2010/archive/2010/04/02/branding-in-sharepoint-2010.aspx

http://msdn.microsoft.com/en-us/library/gg430141.aspx