Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.
Showing posts with label Deploying UserControl in SharePoint 2007.. Show all posts
Showing posts with label Deploying UserControl in SharePoint 2007.. Show all posts

Thursday, December 01, 2011

How to Use SpUserToken in SharePoint 2007 or SharePoint 2010



using System; using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Security;
public partial class WebUserControl : System.Web.UI.UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
SPUserToken sToken = SPContext.Current.Site.SystemAccount.UserToken;
using (SPSite site = new SPSite(http://vlnrm:100/, sToken))
{
using (SPWeb objWeb = site.OpenWeb())
{
SPQuery query = new SPQuery();
query.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
SPList lst = objWeb.Lists["My Links"];
SPListItemCollection myColl = lst.GetItems(query);
if (myColl.Count > 0)
{
this.Controls.Add(new LiteralControl("<Table width='100%' bgcolor='Aqua'>"));
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
this.Controls.Add(new LiteralControl("<Strong>"));
this.Controls.Add(new LiteralControl("Link Name"));
this.Controls.Add(new LiteralControl("</Strong>"));
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
foreach (SPListItem item in myColl)
{
string strHyperLInkTitle = string.Empty;
string strHyperLinkURL = string.Empty;
strHyperLInkTitle = item["HyperLinkTitle"].ToString();
strHyperLinkURL = item["URL"].ToString();
this.Controls.Add(new LiteralControl("<Tr>"));
this.Controls.Add(new LiteralControl("<Td>"));
this.Controls.Add(new LiteralControl("<A href='"+strHyperLinkURL+"'>"+strHyperLInkTitle+"</A>"));
this.Controls.Add(new LiteralControl("</Td>"));
this.Controls.Add(new LiteralControl("</Tr>"));
}
this.Controls.Add(new LiteralControl("<Table>"));
}
}
}
}
}

Wednesday, November 30, 2011

Easy Way to Implement Ajax in SharePoint 2007 with UserControl.



Implementation of Ajax in SharePoint
·         First create one asp.net Ajax website.  You will get Ajax entries in web.config. fill the relevant sections of SharePoint Web.config carefully.
·         Copy ajaxcontroltookit dll to your bin folder.
·         Now copy the ajaxcontroltookit safe control to your SharePoint Web.config.
·         Now open your SharePoint site in SharePoint Designer.
·         Do as it is as the above thing.
·         Now add the register reference tab as below for Ajax Control Toolkit like this.
·         <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="ajaxToolkit" %>
·         Now add ScriptManager after the form tag within a Master Page. Add this as below.
·           <ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" RunAt="Server" />
·         Now take a user control and place an update panel in that and place a grid inside the content template of update panel.
·         Now write your logic in ascx.cs file as shown below.


Ajax Ascx Page
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="AjaxWebUserControl.ascx.cs" Inherits="AjaxWebUserControl" %>
<%@ Register Assembly="AjaxControlToolkit, Version=1.0.20229.20821, Culture=neutral, PublicKeyToken=28f01b0e84b6d53e"
    Namespace="AjaxControlToolkit" TagPrefix="cc1" %>
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
    <asp:GridView ID="dgvTasks" runat="server" AutoGenerateColumns="False" CellPadding="4" PageSize="10"
        ForeColor="#333333" GridLines="Both" AllowPaging="True" OnPageIndexChanging="dgvTasks_PageIndexChanging">
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <Columns>
            <asp:BoundField DataField="Title" HeaderText="Title" />
            <asp:BoundField DataField="Status" HeaderText="Status" />
            <asp:BoundField DataField="Priority" HeaderText="Priority" />
        </Columns>
        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="White" />
    </asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Microsoft.SharePoint;
using System.Security;

public partial class AjaxWebUserControl : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            fillGrid();
        }
    }
    public void fillGrid()
    {
        SPSecurity.RunWithElevatedPrivileges(delegate()
        {
            SPWeb currentWeb = SPContext.Current.Web;
            using (SPSite site = new SPSite(currentWeb.Url))
            {
                using (SPWeb objWeb = site.OpenWeb())
                {
                    SPList lstTasks = objWeb.Lists["Tasks"];
                    SPListItemCollection myColl = lstTasks.Items;
                    if (myColl.Count > 0)
                    {
                        dgvTasks.DataSource = myColl.GetDataTable();
                        dgvTasks.DataBind();
                    }
                }
            }
        });
    }
    protected void dgvTasks_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        dgvTasks.PageIndex = e.NewPageIndex;
        fillGrid();
    }
}
Adding Ajax User Control to a WebPart.
using System;
using System.Runtime.InteropServices;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Serialization;
using System.Security;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using Microsoft.SharePoint.WebPartPages;
using System.Web.Extensions;
[assembly:AllowPartiallyTrustedCallers]
namespace UserControlSample
{
    [Guid("ee10f4a4-4126-4350-967e-e49ea40ee7c7")]
    public class UserControlSample : System.Web.UI.WebControls.WebParts.WebPart
    {
        string strError = string.Empty;
        Control MyControl;
        public UserControlSample()
        {
        }
        protected override void Render(HtmlTextWriter writer)
        {
            writer.Write(strError);
            try
            {
                    MyControl.RenderControl(writer);
               
            }
            catch (Exception ex)
            {
                writer.Write(ex.ToString());
            }
        }
       
        protected override void CreateChildControls()
        {
            try
            {
               
                EnsurePanelFix();
                this.Controls.Clear();
                MyControl = this.Page.LoadControl("\\_LAYOUTS\\Custom UserControls\\AjaxWebUserControl.ascx");
                MyControl.ID = "myControl";
                this.Controls.Add(MyControl);
            }
            catch (Exception ex)
            {
                strError += ex.ToString();
            }
        }
        private void EnsurePanelFix()
        {
            ScriptManager.RegisterStartupScript
              (this,
               typeof(Control),
               "UpdatePanelFixup",
               "_spOriginalFormAction = document.forms[0].action; _spSuppressFormOnSubmitWrapper=true;",
               true);
        }
    }
}