Tuesday, March 13, 2012

Comparing Publishing Dates of Blog Posts

When displaying the list of published posts from a SharePoint Blog, you may want to hide certain elements such as the Publish Date div box if there are multiple posts on the same day. The XSL code below will compare the publish date of the post to the previous post and display or not display the div accordingly.

<xsl:variable name="PrevPosition" select="count(./preceding-sibling::*)" />
<xsl:if test="($Rows/Row[$PrevPosition]/@PublishedDate.MonthDayOnly !=
$thisNode/@PublishedDate.MonthDayOnly) or ($PrevPosition+1 = $FirstRow)">
...
</xsl:if>

Tuesday, February 7, 2012

Changing the SP Ribbon background color using CSS

To change the background color of SharePoint's ribbon, override the following styles from the corev4.css file.


.ms-SpLinkButtonActive
{
background-color:orange !important;
}

.ms-welcomeMenu a:hover
{
background-color: orange !important;
}


.ms-siteactionsmenu .ms-siteactionsmenuhover
{
background-color: orange !important;
color:#000000 !important;
}


.ms-cui-ribbonTopBars
{
background: orange url() repeat-x !important;
padding-top:0px;
}

Thursday, December 22, 2011

Setting the forecolor of a control based upon the backcolor

The code below will set the forecolor or font color of a control, such as a label, to white or black, depending on the background color.
System.Drawing.Color foreColor = System.Drawing.Color.Black;

// all colors coming in are in Hex format. No Known colors but we'll check anyway.
int r;
int g;
int b;

if (backColor.IsKnownColor)
{
r = backColor.R;
g = backColor.G;
b = backColor.B;
}
else
{
r = int.Parse(backColor.ToString().Substring(8, 2), System.Globalization.NumberStyles.HexNumber);
g = int.Parse(backColor.ToString().Substring(10, 2), System.Globalization.NumberStyles.HexNumber);
b = int.Parse(backColor.ToString().Substring(12, 2), System.Globalization.NumberStyles.HexNumber);
}

int changeToWhite = 0;
if (r <= 128)
{
changeToWhite ++;
}
if (g <= 128)
{
changeToWhite ++;
}
if (b <= 128)
{
changeToWhite ++;
}
if (changeToWhite > 2)
{
foreColor = System.Drawing.Color.White;
}

Tuesday, October 18, 2011

Using AJAX in a SharePoint webpart - UpdatePanel control

To take advantage of AJAX and partial page rendering in a SP2010 webpart, add your radio button, dropdown list, etc. controls to an UpdatePanel for a better user experience.

The CGCC-Parent.master file already contains the ScriptManager control so there is no need to add this to your webpart.

1. Create an instance of the panel. Set the UpdateMode to conditional.

updatePanel2 = new UpdatePanel();
updatePanel2.UpdateMode = UpdatePanelUpdateMode.Conditional;
2. Add your controls to the updatePanel. Make sure you add the controls to the ContentTemplateContainer.

updatePanel1.ContentTemplateContainer.Controls.Add(creditRadio);
updatePanel1.ContentTemplateContainer.Controls.Add(creditRequired);
updatePanel1.ContentTemplateContainer.Controls.Add(campusQ1);
updatePanel1.ContentTemplateContainer.Controls.Add(creditCheckBox);

3. Finally, and add your panel to the list of controls.
Controls.Add(updatePanel1);

Thursday, September 22, 2011

More on the TextBox Counter by SKMControls

I found a free TextBox counter by SKMControls that counts the amount of characters in a multiline asp:TextBox control. This is useful for Asp.NET pages with Note fields where end users tend to write alot but yet you have limits to how many characters the database field will hold.

First add the file skmcontrols2.dll to the bin folder.

In your aspx .NET page, register the control.


<% @ Register Assembly="skmControls2" Namespace="skmControls2" TagPrefix="skm" %>

Next, add your control to the page.

<skm:TextBoxCounter runat="server" ForeColor="GrayText" WarningPercentage="80" MaxCharacterLength="7900" TextBoxControlId="NoteTextBox" ID="TextBoxCounter1"></skm:TextBoxCounter>

The TextBoxControlID property is used to bind the control to the multi-line textbox on your page.

Tuesday, July 26, 2011

Using the asp:ListView control in SharePoint

Below is example code implementing the asp:ListView control in a SharePoint page. Bound data comes from the SPDataSource control.

The SelectCommand is in the CAML query language which is XML based. The query looks for all Calendar events ending today or later, row limit of 4, sorted by start date. You can also use parameters for a dynamic SelectCommand:

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.webcontrols.spdatasource.selectparameters.aspx

<asp:ListView ID="ListView1" runat="server" DataSourceID="spDataSource1">
<LayoutTemplate>
<div style="background-color: #fffddd; padding: 10px; width: 200px">
<asp:PlaceHolder ID="itemPlaceholder" runat="server"></asp:PlaceHolder>
</div>
</LayoutTemplate>
<ItemTemplate>
<p style="font-size:small;">
<a href="/examples/Lists/Events//DispForm.aspx?ID=<%# Eval("ID") %>" style="color:#787878"><span style='text-decoration:none;<%# Eval("Category").Equals("Holiday") ? "font-weight:bold" : "font-weight:normal" %>'><%# Eval("DisplayDateTime").ToString().Substring(8) %></span><br />
<span style="font-style:italic"><%# Eval("Title") %></span></a></p>
</ItemTemplate>
</asp:ListView>

<SharePointWebControls:SPDataSource runat="server" ID="spDataSource1" DataSourceMode="List" SelectCommand="<Query><Where><Geq><FieldRef Name='EndDate' /><Value Type='DateTime'><Today /></Value></Geq></Where><OrderBy><FieldRef Name='EventDate' Ascending='True' /></OrderBy><RowLimit>4</RowLimit></Query>">
<SelectParameters> <asp:Parameter Name="WebUrl" DefaultValue="/examples/" /> <asp:Parameter Name="ListName" DefaultValue="Events" /> </SelectParameters>
</SharePointWebControls:SPDataSource>

Thursday, July 21, 2011

Creating an SPDataSource .NET control

To use an SP List as a data source for a .NET web control, declare an SPDataSource as shown:

<SharePointWebControls:SPDataSource runat="server" ID="spDataSource1" DataSourceMode="List" SelectCommand="<Query><Where><Eq><FieldRef Name='Section'/><Value Type='Choice'>Get Started</Value></Eq></Where><OrderBy><FieldRef Name='SortOrder' Ascending='True' /></OrderBy></Query>">
<SelectParameters> <asp:Parameter Name="WebUrl" DefaultValue="/student-affairs/enrollment/" /> <asp:Parameter Name="ListName" DefaultValue="Transfer" /> </SelectParameters>
</SharePointWebControls:SPDataSource>


The SelectCommand property allows for filtering and sorting of results.

The ID for this data source will then go inot the DataSourceID field of the web control that uses the SP List, as shown below:

<asp:GridView ID="DataGrid1" runat="server" DataSourceID="spDataSource1">
...
</asp:GridView>