Showing posts with label SPGridView. Show all posts
Showing posts with label SPGridView. Show all posts

Friday, 8 June 2012

Add Presence Icon to SPGridview

I spent hours trying to figure out why my presence image only appears for the last instance of the particular user, and I finally figured out why, id has to be unique.
to show image presence, I attached to rowdatabound event with the required javascript

       protected void gvProjOverview_RowDataBound(object sender, GridViewRowEventArgs e)
        { 
            if(e.Row.DataItem!=null)
            {
                SPWeb web = SPContext.Current.Web; 

                SPUser user = web.EnsureUser("DOMAIN\login");
 
                e.Row.Cells[4].Text = ""
                    + " "
                    + ""
                    + "" + user.Name +  ""
                    + "";
            }
        }

id='imn_" + e.Row.RowIndex <- this was where I had used user.ID which was not unique and thus the presence icon was only showing for one of the available users.

The lack of distinct ID causes some presence icon to disappear, that is, if user Andy is on two of the rows, the presence icon only appears for the last row where Andy is in.

Monday, 4 June 2012

SPMenuField disappears when/after Edit Mode

There has been an issue that has been bugging me when I create an SPMenuField, editing the page will result in the menu field columns disappearing.

After spending hours trialing and erroring, it was resolved by adding a .DataBind() in the Page_Load method.

UserControl.ascx.cs

        protected void Page_Load(object sender, EventArgs e)
        { 
          gvPPL.DataBind();
        }

UserControl.ascx


<SharePoint:MenuTemplate ID="gvPPLMenu" runat="server">
    <SharePoint:MenuItemTemplate ID="editMenu" runat="server" Text="Edit" ClientOnClickNavigateUrl="/_layouts/BPOSproperties/ProjectSettings.aspx?type=edit&url=%EDIT%" />
</SharePoint:MenuTemplate>
<SharePoint:SPGridView ID="gvPPL" runat="server" AutoGenerateColumns="false" DataSourceID="pplDataSource">
    <Columns>
        <SharePoint:SPMenuField HeaderText="NameMenu" TextFields="Name" MenuTemplateId="gvPPLMenu"
        NavigateUrlFields="Email" NavigateUrlFormat="{0}" TokenNameAndValueFields="EDIT=Email" />
        <SharePoint:SPBoundField HeaderText="Name" DataField="Name"  />
    </Columns>
</SharePoint:SPGridView>

<asp:ObjectDataSource ID="pplDataSource" runat="server" TypeName="BPOSsite.PeopleManager, BPOSsite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9ab07f514f0925b" SelectMethod="GetPeople"  />

People.cs


namespace BPOSsite
{
    public class People
    {
        private string name;
        private string email;

        public People(string n, string e)
        {
            this.email = e;
            this.name = n;
        }

        public string Name
        {
            get { return name; }
            set { name = value; }
        }

        public string Email
        {
            get { return email; }
            set { email = value; }
        }
    }

    public class PeopleManager
    {
        public List<People> GetPeople()
        {
            List<People> list = new List<People>();
            list.Add(new People("John", "john@mail.com"));
            list.Add(new People("Nick", "nick@mail.com"));
            list.Add(new People("Tracy", "tracy@mail.com"));
            list.Add(new People("Dick", "dick@mail.com"));
            return list; 
        }    
    }
}

Friday, 1 June 2012

ObjectDataSource TypeName not found

In sharepoint i had to list ObjectDataSource this way

<asp:ObjectDataSource ID="pplDataSource" runat="server" TypeName="MyNameSpace.PeopleManager, MyNameSpace, Version=1.0.0.0, Culture=neutral, PublicKeyToken=c9ab07f514f0925b" SelectMethod="GetPeople"  />

else I will get 'TypeName not found' error.

Wednesday, 14 March 2012

SharePoint 2010 REST with ASP.NET

From http://extendtheenterprise.com/2010/11/03/using-sharepoint-2010-rest-apis-in-asp-net/
with some changes to make it work for me.



Default.aspx


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SPRest._Default" %>

<!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></title>
<style type="text/css">
table.gridtbl {
background-color: orange;
font-family: tahoma;
}
table.gridtbl TR {
background-color: white;
font-family: tahoma;
}
table.gridtbl TH {
background-color:Gray;
PADDING:4px;
color: White;
font-family: tahoma;
}
table.gridtbl TD {
PADDING:4px;
font-family: tahoma;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CSSClass="gridtbl" CellSpacing="1" GridLines="None"/>
</div>
</form>
</body>
</html>





Default.aspx.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SPRest.SUB1DataContext;
using System.Net;
namespace SPRest
{
public partial class _Default : System.Web.UI.Page
{
SPRest.SUB1DataContext.SUB1DataContext context = new SPRest.SUB1DataContext.SUB1DataContext(new Uri("http://blah/_vti_bin/listdata.svc"));

protected void Page_Load(object sender, EventArgs e)
{
context.Credentials = CredentialCache.DefaultNetworkCredentials;
BindData();
}

protected void BindData()
{
var custItems = from c in context.Customer.ToList()
join d in context.Purchases.ToList()
on c.CustomerID equals d.PurchaserID into TMP
from subTMP in TMP.DefaultIfEmpty()
select new
{
c.CustomerID,
c.Name,
OrderNum =(subTMP==null)? "NA" : subTMP.OrderNumber,
Product = (subTMP==null)? "NA" : subTMP.Product,
Qty = (subTMP==null)? null : subTMP.Quantity,
Price = (subTMP==null)? null : subTMP.Price
};

GridView1.DataSource = custItems;
GridView1.DataBind();
}
}
}



Result

Monday, 5 March 2012

Customizing SPGridView style (with hover alt rows)

ascx


<style type="text/css">

.GridViewHeaderStyle {
background-color:#c3dde0;
border-width: 1px;
padding: 8px;
border-style: solid;
border-color: #a9c6c9;
}

.GridViewAltRowStyle
{
background-color: #F0F8FF;
font-weight: bold;
color: black;
}

.GridRowStyle
{
background-color: #E6E6FA;
font-weight: bold;
color: black;
}

.GridViewAltRowStyle:hover, .GridRowStyle:hover
{
background-color: yellow;
color: black;
font-weight: bold;
}

</style>


<SharePoint:SPGridView ID="myGridView"
runat="server" AutoGenerateColumns="false" >
</SharePoint:SPGridView>




ascx.cs


public partial class SPGridViewTestUserControl : UserControl
{
protected void Page_Load(object sender, EventArgs e)
{
string[] fields = { "ID","Name","Country","Total"};
foreach (string s in fields)
{
BoundField bf = new BoundField();
bf.DataField = s;
bf.HeaderText = s;
myGridView.Columns.Add(bf);
}


myGridView.GroupField = "Name";
myGridView.AllowGrouping = true;
myGridView.AllowGroupCollapse = true;
myGridView.AlternatingRowStyle.CssClass = "GridViewAltRowStyle";
myGridView.RowStyle.CssClass = "GridRowStyle";

myGridView.DataSource = SelectData();
myGridView.DataBind();

// if this is set before databind, it won't include the column where the grouping is (top left)
foreach (DataControlField dcf in myGridView.Columns)
{
dcf.HeaderStyle.CssClass = "GridViewHeaderStyle";
}
}


public DataTable SelectData()
{
DataTable dataSource = new DataTable();

dataSource.Columns.Add(new DataColumn("ID", Type.GetType("System.Int32")));
dataSource.Columns.Add(new DataColumn("Name", Type.GetType("System.String")));
dataSource.Columns.Add(new DataColumn("Country", Type.GetType("System.String")));
dataSource.Columns.Add(new DataColumn("Total", Type.GetType("System.Int32")));

dataSource.Rows.Add(1, "Jack", "USA", 10000);
dataSource.Rows.Add(2, "Jack", "Thailand", 15000);
dataSource.Rows.Add(3, "Jack", "Australia", 5000);
dataSource.Rows.Add(4, "Sam", "USA", 7000);
dataSource.Rows.Add(5, "Sam", "Thailand", 30000);
dataSource.Rows.Add(6, "Sam", "Australia", 8700);
dataSource.Rows.Add(7, "Andy", "USA", 3000);
dataSource.Rows.Add(8, "Andy", "Thailand", 50000);
dataSource.Rows.Add(9, "Andy", "Australia", 25000);

return dataSource;
}
}




How it looks,

Monday, 27 February 2012

Load a List Table dynamically with columns

This took me a while to figure out, for predefined fields like ID, Modified, Created, Title, it will still have a Hidden=True even though it is not shown in AllItems.aspx. Thus I have to check it by using the Exists method.


public void loadHeaderActionTable()
{
SPWeb web = SPContext.Current.Web;
SPList actionList = web.Lists[ActionListName];
SPListItemCollection actions = actionList.GetItems(myActionListView);

TableRow header = new TableRow();
foreach (SPField fItem in actions.Fields)
{

if (!fItem.Hidden
&& myActionListView.ViewFields.Exists(fItem.InternalName))
{

TableCell cItem = new TableCell();
cItem.Text = fItem.Title.ToString();
cItem.CssClass = "ms-vh-div";
header.Cells.Add(cItem);
myActionColumnTitle.Add(cItem.Text);

}
}
tblACList.Rows.Add(header);

}