Monday, August 15, 2011

When a custom webpart gives the runtime exception while loading

When a custom webpart gives the runtime exception while loading , you can slove by two ways

1) standard way ( retracting the solution)

2) adding ?contents=1 in the url and later delete the custom webpart


Sharepoint smoking...............

Wednesday, May 18, 2011

Getting the User profile

Note : Got the code while search for User profile in Net. I have changed only the Red Line because the code I got, was getting the context as null.
 
 
 
 
private UserProfile GetUserInfo(string AccountName)
{
UserProfile profile = null;
SPServiceContext serviceContext = SPServiceContext.GetContext(SPContext.Current.Site);
UserProfileManager profileManager = new UserProfileManager(serviceContext);
if (AccountName != string.Empty)
{
profile = profileManager.GetUserProfile(AccountName);
}
else
{
profile = profileManager.GetUserProfile(SPContext.Current.Web.CurrentUser.RawSid);
}
return profile;
}
 
 
Sharepoint Smoking..........
Dharani

Wednesday, May 11, 2011

Retrieving Picture URL or User Info from sharepoint webservices


First add the web reference to the userprofileservice.asmx

http://serverURL/_vti_bin/userprofileservice.asmx



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.Net;
using ConsoleApplicationSharepointTesting.WebServiceMyReference;  (Name of the Web reference)    

namespace ConsoleApplicationSharepointTesting
{
class Program
{
static void Main(string[] args)
{
PropertyData[] userDetails;
UserProfileService userprof = new UserProfileService();
SPSecurity.RunWithElevatedPrivileges(delegate()
{

userprof.PreAuthenticate = true;
userprof.Credentials = new NetworkCredential("UserName", "Password", "Doamin");
userDetails = userprof.GetUserProfileByName("Domain\UserName");

if (userDetails != null)
{

Console.WriteLine(userDetails[15].Name + " Path of the Image -------" + userDetails[15].Values[0].Value.ToString());

}

Console.Read();
});


Where index 15 refers to the PictureURL

2 refers to the FirstName
4 refers to the LastName

For NetworkCredential(....) use System.Net Namespace

By using foreach loop we can get all the details of the user.
kindly do check in the loop

userDetails[Index].Values have a value, otherwise we will be getting the index out of range runtime error.




Sharepoint smoking ........
Dharani






Monday, April 25, 2011

Pass values in querystring with the hyperlink field inside the gridview

Hi,
I got the requirement to pass the two values in the form of query string of the hyperlink field in the grid view.One parameter is fron the database and another one is server side value.
Here is the solution for the above requirement.
FirstPage:
<%@ Page Language="C#"  %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
    public string Name = "Prabhakara";
 
    public string GetURL(object productName)
    {
        string url = string.Empty;
        url = string.Format("Testing.aspx?ProductName={0}&Name={1}", productName.ToString(), Name);
        return url;
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="Gridview1" runat="server" DataSourceID="SqlDataSource1" DataKeyNames="ProductID"
            AllowPaging="True" AutoGenerateColumns="False">
            <Columns>
                <asp:BoundField HeaderText="ProductID" DataField="ProductID" ReadOnly="True" SortExpression="ProductID" />
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:HyperLink ID="hlMenuItem" runat="server" NavigateUrl='<%# GetURL(DataBinder.Eval(Container.DataItem, "ProductName")) %>'
                            Text='<%# DataBinder.Eval(Container.DataItem, "ProductName")%>'>
                        </asp:HyperLink>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName] FROM [Alphabetical list of products]">
        </asp:SqlDataSource>
        <asp:Label ID="Label1" runat="server" Text="<%= Name %>"></asp:Label>
    </div>
    </form>
</body>
</html>
TestingPage:

<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
    protected void btnBack_Click(object sender, EventArgs e)
    {
        Response.Redirect("HyperLinkExample.aspx");
    }
 
    protected void Page_Load(object sender, EventArgs e)
    {
         Label1.Text = Request.QueryString["ProductName"].ToString() + " and " + Request.QueryString["Name"].ToString();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
        <br />
        <asp:Button ID="btnBack" runat="server" Text="Back" OnClick="btnBack_Click" />
    </div>
    </form>
</body>
</html>


HappyCoding....

Friday, April 8, 2011

Gridview update with sqldatasource using stored procedure

Hi,
Here i have a complete example of how to use the sqldatasource and gridview to update the records using the stored procedure.
Aspx page with code:
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
 
    protected void grvEmployees_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string employeeID = grvEmployees.DataKeys[e.RowIndex].Value.ToString();
 
        string gender = ((DropDownList)grvEmployees.Rows[e.RowIndex].FindControl("DropDownList1")).SelectedValue.ToString();
 
        empDataSource.UpdateParameters["Gender"].DefaultValue = gender;
        empDataSource.UpdateParameters["EmployeeID"].DefaultValue = employeeID.ToString();
        empDataSource.Update();
    }
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Dropdownlist in EditItemTemplate</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grvEmployees" runat="server" AutoGenerateColumns="False" DataKeyNames="EmployeeID"
            DataSourceID="empDataSource" AllowPaging="true" PageSize="10" AutoGenerateEditButton="True"
            OnRowUpdating="grvEmployees_RowUpdating">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="EmployeeID" InsertVisible="False" SortExpression="EmployeeID">
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("EmployeeID") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("EmployeeID") %>'></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Gender" SortExpression="Gender">
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("Gender") %>'></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# DataBinder.Eval(Container.DataItem,"Gender") %>'>
                            <asp:ListItem Value="M">M</asp:ListItem>
                            <asp:ListItem Value="F">F</asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
 
    </div>
    <asp:SqlDataSource ID="empDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>"
        SelectCommand="SELECT [EmployeeID], [Gender] FROM [AdventureWorks].[HumanResources].[Employee]"
        UpdateCommand="[HumanResources].[dbo.uspUpdateEmployeeGender]"
        UpdateCommandType="StoredProcedure">
        <UpdateParameters>
            <asp:Parameter Type="Char" Name="Gender"></asp:Parameter>
            <asp:Parameter Name="EmployeeID"></asp:Parameter>
        </UpdateParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>

Srored procedure:

USE [AdventureWorks]GO/****** Object: StoredProcedure [HumanResources].[dbo.uspUpdateEmployeeGender] Script Date: 04/08/2011 12:34:21 ******/SET
GO
SET
GO ANSI_NULLS ON QUOTED_IDENTIFIER ON-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================CREATE
@EmployeeID [int]
@Gender [nchar] PROCEDURE [HumanResources].[dbo.uspUpdateEmployeeGender], (1) AS
BEGIN
UPDATE [HumanResources].[Employee] SET [Gender] = @Gender WHERE [EmployeeID] = @EmployeeID;END
GO
Happy coding...

Friday, March 25, 2011

Highliting Row in DataGrid on mouse over and adding row click eventt to gridview

Here is the example for adding mouseover effect and also adding row click event to the grid rows.

Aspx page with code behind:
<%@ Page Language="C#" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<script runat="server">  
  
    protected void grvContact_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
        if (e.Row.RowType == DataControlRowType.DataRow) 
        { 
            e.Row.Attributes.Add("onmouseover""this.style.backgroundColor='#aaaaaa';this.style.cursor='hand'"); 
            e.Row.Attributes.Add("onmouseout""this.style.backgroundColor='ffffff';"); 
            e.Row.Attributes.Add("onclick", ClientScript.GetPostBackClientHyperlink(this.grvContact, "Select$" + e.Row.RowIndex)); 
        } 
    } 
 
    //this code to avoid the validation request issue
    protected override void Render(HtmlTextWriter writer) 
    { 
        for (int i = 0; i < this.grvContact.Rows.Count; i++) 
        { 
            ClientScript.RegisterForEventValidation(this.grvContact.UniqueID, "Select$" + i); 
        } 
        base.Render(writer); 
    } 
 
    protected void grvContact_SelectedIndexChanged(object sender, EventArgs e) 
    { 
        Label1.Text = "Row with the contact ID <b>" + this.grvContact.SelectedDataKey.Value.ToString() + " </b> clicked."; 
    } 
</script> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head id="Head1" runat="server"> 
    <title></title> 
</head> 
<body> 
    <form id="form1" runat="server"> 
    <div> 
        <asp:GridView ID="grvContact" runat="server" DataSourceID="SqlDataSource2" DataKeyNames="ContactID" 
            AutoGenerateColumns="False" 
            AllowPaging="true" OnRowDataBound="grvContact_RowDataBound"  
            onselectedindexchanged="grvContact_SelectedIndexChanged"> 
            <Columns> 
                <asp:BoundField DataField="ContactID" HeaderText="ContactID" /> 
                <asp:BoundField DataField="FirstName" HeaderText="FirstName" /> 
                <asp:BoundField DataField="MiddleName" HeaderText="MiddleName" /> 
                <asp:BoundField DataField="LastName" HeaderText="LastName" /> 
            </Columns> 
        </asp:GridView> 
    </div> 
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:AdventureWorksConnectionString %>" 
        SelectCommand="SELECT TOP 100 [ContactID], [FirstName], [MiddleName], [LastName] FROM [AdventureWorks].[Person].[Contact]"> 
    </asp:SqlDataSource> 
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
    </form> 
</body> 
</html>

Friday, March 18, 2011

Filter datagrid based on some condition

Hi,

Here is the code to filter or search the gridview based on some condition.Aspx page:

<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Text box Search example</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <table border="1px solid">
            <tr>
                <td>
                    Enter product Name:
                </td>
                <td>
                    <asp:TextBox ID="txtProductName" runat="server"></asp:TextBox>
                </td>
                <td>
                    <asp:Button ID="btnSearch" runat="server" Text="Search" />
                </td>
            </tr>
            <tr>
                <td colspan="3">
                    <asp:GridView ID="grvProducts" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                        DataKeyNames="ProductID" DataSourceID="SqlDataSource1" ShowFooter="True">
                        <Columns>
                            <asp:BoundField DataField="ProductID" HeaderText="ProductID" SortExpression="ProductID" />
                            <asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
                            <asp:BoundField DataField="UnitsInStock" HeaderText="UnitsInStock" SortExpression="UnitsInStock" />
                            <asp:BoundField DataField="UnitPrice" HeaderText="UnitPrice" SortExpression="UnitPrice" />
                        </Columns>
                    </asp:GridView>
                </td>
            </tr>
        </table>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
            SelectCommand="SELECT [ProductID], [ProductName], [UnitsInStock], [UnitPrice] FROM [Alphabetical list of products] WHERE ([ProductName] LIKE '%' + @ProductName + '%')">
            <SelectParameters>
                <asp:ControlParameter ControlID="txtProductName" DefaultValue="%" Name="ProductName"
                    PropertyName="Text" Type="String" />
            </SelectParameters>
        </asp:SqlDataSource>
    </div>
    </form>
</body>
</html>