Pages

Tuesday, July 28, 2009

10 Useful Code Snippets for Web Developers

This is a great article. I've copied the content here (so I don't lose it) but it was originally published here: http://woork.blogspot.com/2009/07/10-useful-code-snippets-for-web.html






1. CSS Print Framework
Hartija is an universal Cascading Style Sheets Framework for web printing. To use this framework download the CSS file here and use this line of code into your web pages:

<link rel="stylesheet" href="print.css" type="text/css" media="print">


2. CSS @font-face
This snippet allows authors to specify online custom fonts to display text on their webpages without using images:

@font-face {
font-family: MyFontFamily;
src: url('http://');
}


3. HTML 5 CSS Reset
Richard Clark made a few adjustments to the original CSS reset released from Eric Meyers.


4. Unit PNG Fix
This snippet fixes the png 24 bit transparency with Internet Explorer 6.


5. Tab Bar with rounded corners
This code illustrates how to implement a simple tab bar with rounded corners:



6. PHP: Use isset() instead of strlen()
This snippet uses isset() instead strlen() to verify a PHP variable (in this example $username) is set and is at least six characters long. (via Smashing Magazine).

<?php
if (isset($username[5])) {
// Do something...
}
?>


7. PHP: Convert strings into clickable url
This snippet is very useful to convert a string in a clickable link. I used this snippet for several tutorials; for example take a look at this link Simple PHP Twitter Search ready to use in your web projects where I used this snippet to convet into a clickable link all textual links contained in a tweet.

<? php
function convertToURL($text) {
$text = preg_replace("/([a-zA-Z]+:\/\/[a-z0-9\_\.\-]+"."[a-z]{2,6}[a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/"," <a href=\"$1\" target=\"_blank\">$1</a>", $text);
$text = preg_replace("/[^a-z]+[^:\/\/](www\."."[^\.]+[\w][\.|\/][a-zA-Z0-9\/\*\-\_\?\&\%\=\,\+\.]+)/"," <a href="\\" target="\">$1</a>", $text);
$text = preg_replace("/([\s|\,\>])([a-zA-Z][a-zA-Z0-9\_\.\-]*[a-z" . "A-Z]*\@[a-zA-Z][a-zA-Z0-9\_\.\-]*[a-zA-Z]{2,6})" . "([A-Za-z0-9\!\?\@\#\$\%\^\&\*\(\)\_\-\=\+]*)" . "([\s|\.|\,\<])/i", "$1<a href=\"mailto:$2$3\">$2</a>$4",
$text);
return $text;
}
?>


8. jQuery: Ajax call
This is the most simple way to implement an Ajax call using jQuery. Change formId and inputFieldId with the ID of the form and input field you have to submit:

<script type="text/javascript">
$(document).ready(function(){
$("form#formId").submit(function() {
inputField = $('#inputFieldId').attr('value');
$.ajax({
type: "POST",
url: "yourpage.php",
cache: false,
data: "inputField ="+ inputField,
success: function(html){
$("#ajax-results").html(html);
}
});
return false;
});
});
</script>


9. CSS Layouts Collections
This page contains a collection of over 300 grids and CSS layout ready to use in your projects. Take a look, it's very useful.


10. Simple versatile multilevel navigation Menu
Several months ago I found this simple code (HTML + CSS + JavaScript for jQuery) to implement a versatile multilevel navigation menu (please send me the original source if you find it!). I think it's the simpler and faster way to do that. The result is something like this:


The only thing you have to do is to create nested &ltul> lists into a main list with id="nav", in this way:

<ul id="nav">
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a>
<ul>
<li><a href="#">Link 3.1</a></li>
<li><a href="#">Link 3.2</a></li>
<ul>
</li>
</ul>


...and use this basic CSS code (you have to modify it to customize the layout of this menu with the style of your site!):

#nav, #nav ul{
margin:0;
padding:0;
list-style-type:none;
list-style-position:outside;
position:relative;
line-height:26px;
}
#nav a:link,
#nav a:active,
#nav a:visited{
display:block;
color:#FFF;
text-decoration:none;
background:#444;
height:26px;
line-height:26px;
padding:0 6px;
margin-right:1px;
}
#nav a:hover{
background:#0066FF;
color:#FFF;
}
#nav li{
float:left;
position:relative;
}
#nav ul {
position:absolute;
width:12em;
top:26px;
display:none;
}
#nav li ul a{
width:12em;
float:left;
}
#nav ul ul{
width:12em;
top:auto;
}
#nav li ul ul {margin:0 0 0 13em;}
#nav li:hover ul ul,
#nav li:hover ul ul ul,
#nav li:hover ul ul ul ul{display:none;}
#nav li:hover ul,
#nav li li:hover ul,
#nav li li li:hover ul,
#nav li li li li:hover ul{display:block;}

...and this is the JavaScript code for jQuery you have to copy in the tag <head> of the pages that use this menu:

<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<script type="text/javascript">
function showmenu(){
$("#nav li").hover(function(){
$(this).find('ul:first').css({visibility:"visible", display:"none"}).show();
}, function(){
$(this).find('ul:first').css({visibility:"hidden"});
});
}

$(document).ready(function(){
showmenu();
});
</script>







Thanks again to Antonio Lupetti for putting this together.

Tuesday, July 21, 2009

Customizing the Content Query Web Part (CQWP)


One of the nicest webparts available in SharePoint 2007 is the Content Query Web Part (a.k.a. CQWP). This web part allows you to query data from any list on the same site collection, and display those list items. This works great if you want to show corporate news on a team’s site, or in my case, show blog entries on various sites throughout the organization.

The scenario: a senior leader has started a blog dedicated to the new project management system that’s being rolled out. Obviously people can subscribe via RSS to the blog, but we also want to display the most recent entry on a project team’s SharePoint site. To do so, we’re going to use the CQWP, and customize the item style, so that we’re displaying the content how we want it to appear.

The setup - create a blog site in SharePoint, and create a web part page (or use an existing one) that is not on the blog site, to host the CQWP.

First, the CQWP.

Edit your host page, and add a Content Query Web Part to one of the zones.

cqwp_01.png

After it’s added, click ‘Modify Shared Web Part’ to open the tool pane.

Expand the Query section, and choose “Show items from the following list”, then click “Browse” and navigate to your blog site, and select the “Posts” list.

cqwp_02.png

Scroll down and expand the “Presentation” section, and locate “Styles” section. Choose “Title and Description” from the “Item Style” pull down.

cqwp_03.png

Click Apply, then OK to get back to your page.

cqwp_04.png

Notice that the CQWP is only showing the title of the blog post, not the body.

Now obviously, we want to display the body of the post which is Rich HTML, and we want to show what day/time the post was submitted. To get that to work, we need to know the internal field name SharePoint uses for the Body field of a blog post, and the Published field of a blog post. Then we’ll edit the XSL Item Style sheet to include the new fields.

Getting the internal field names

So the two fields besides Title we want to display are Body and Published. To find the internal field name of those fields, do the following:

  • Navigate to the Blog site

  • Click ‘View All Site Content’ on the left hand navigation bar

  • Select the ‘Posts’ list

  • Click the ‘Settings’ button, then choose ‘List Settings’

  • On the List Settings page, you’ll see a section called Columns

  • Click on the column you want to add - 'Body'

  • Now, look at the address bar, and find where it shows the field name “&Field=Body”

    cqwp_05.png


  • Repeat the steps above for 'Published', and you’ll notice that the internal column name for 'Published' is 'PublishedDate'



Now that we have the internal column names, we need to decide what type of fields they are. In the XSL sheet, we must choose one of these field types for each field.


  • Text

  • Note

  • Number

  • Currency

  • Integer

  • Boolean

  • DateTime

  • Threading

  • Lookup

  • Choice

  • URL

  • Counter

  • DisplayOnly (DisplayOnly field type has no storage of its own)

  • RichHTML

  • Image



In this example, Body is going to be “RichHTML” and PublishedDate is going to be “DateTime”.

Editing the CQWP

Now that we have the field names and their types, we need to edit the CQWP to make sure the part is pulling in all the fields we need.

Go to your web part, and on the menu select ‘Export’. Pick a file name for your part, and choose a location to save it.

Edit the .webpart file using Notepad or some other text editor, and locate the line that reads:

<property name="CommonViewFields" type="string" />

Replace this line with the line below:

<property name="CommonViewFields" type="string">Body, RichHTML;PublishedDate, DateTime</property>

If you want the webpart to pull more fields, just keep adding them separated by a semi-colon.

Save the part, and go pack to your web part page.

Remove the CQWP that is there, and then import the .webpart file you just edited.

Click ‘Add Web Part’, then select “Advanced Web Part Gallery and Options”

cqwp_06.png
cqwp_07.png

After you’ve imported and added your part, you won’t see anything different. We’ve told the CQWP to pull in our extra fields, but we still need to edit the XSL stylesheet to display those fields.

Editing the XSL Stylesheet

Open SharePoint Designer, and open the root site of your site collection.

Choose the ‘Style Library’ document library from the root site, open the XSL Style Sheets folder, and edit ITEMSTYLE.XSL. This unghosts the page, but we can reset it if we want at any time.

Each <xsl:template> element is a “style” to display an item in the CQWP. Copy any of the styles, and paste it after the last closing </xsl:template> element and before the closing <xsl:stylesheet> tag.

Edit the name and match attributes of your new template:

<xsl:template name=”MyCustomStyle” match=”Row[@Style=’MyCustomStyle’]” mode=”itemstyle”>

Now, in the last section of your newly created <xsl:template>, add a value-of xsl tag to display the fields you want appropriately.

<xsl:value-of select=”@PublishedDate” />

<xsl:value-of select=“@Body” disable-output-escaping=”yes” />

NOTE: Notice the disable-output-escaping=“yes” attribute of the @Body element. This tells the webpart to output HTML rather than raw text. - so if you put HTML into the body of your post, it will display as HTML.

Save the file. Return to the site (in the browser) and refresh the page. Open the Web Part Tool Pane for the CQWP and expand Presentation, then under Styles change the Item Style to the new custom style in the drop down. Select Apply.

With closer inspection of the ITEMSTYLE.xsl, you’ll notice that each <xsl:template> is wrapped in HTML - simply change that HTML to however you want to display your item, and you’re good to go.


~~~~~ Credit ~~~~~

I couldn’t have figured this out with out having read these three posts... Big thanks to the authors.
http://www.heathersolomon.com/blog/articles/CustomItemStyle.aspx

http://blogs.msdn.com/ecm/archive/2006/10/25/configuring-and-customizing-the-content-query-web-part.aspx

http://www.sharepoint-tips.com/2007/01/showing-description-of-page-in-content.html

Wednesday, July 15, 2009

Third Party SharePoint Workflow Actions

I stumbled on this Codeplex project and our server administrators were nice enough to add the project to our SharePoint farm. The tool adds 13 new workflow actions to the Workflow Wizard in SharePoint Designer. I’ve found the new actions to be very helpful, like sending an email with a list item’s attached files attached to the message, and getting the innerXML of an InfoPath element.

Link is below, and I’ve pasted the list of actions for reference as well. Worth installing in my opinion.

http://spdactivities.codeplex.com/

Available Activities
  1. Send Email with HTTP File attachment - Allows sending emails with attachments retrieved using a web request
  2. Send Email with List Item attachments - Allows sending list item attachments as files attached to an email
  3. Start Another Workflow - Starts another workflow associated with a list item
  4. Grant Permission on Item - Allows granting of specified permission level on a spicified item
  5. Delete List Item Permission Assigment - Allows deleting of specified permission level assigment for a given user
  6. Reset List Permissions Inheritance - removes any unique permissions assigned to an item by inheriting list permissions
  7. Is User a member of a SharePoint group - Checks if a given user is part of given sharepoint group
  8. Is Role assigned to User - Checks if a user role is already assigned on the current list item
  9. Lookup user info - allows to lookup properties in site's user information list for a given login
  10. NEW! Copy List Item Extended Activity - Allows copying/moving list items and files cross site.
  11. NEW! Send Email Extended - Enhaced version of the OOTB activity. Allows you to specify the sender. Also does not break links in body.
InfoPath activities - allow to query/change InfoPath form data using XPath

  1. Get InfoPath field inner text

  2. Get InfoPath field inner xml

Thursday, July 9, 2009

Concatenating a repeating field into a single string in InfoPath 2007

Here's the scenario: You have an InfoPath form that contains a repeating table, and that table is used to capture names and email addresses. You'd like to create a workflow in SharePoint Designer that will email some information to the users you entered in the form. The only problem is SharePoint Designer doesn't have a loop function in the workflow creation wizard.

To get around the issue, we're going to use a little xpath function to concatenate the email address fields in the repeating table together, separated by a semi-colon - this will let us drop that list right into the TO line of an email.

The setup:
  • Create a new InfoPath form using a blank template.
  • Add the following fields to the main data source:

message_subject
message_body
people (group)
     person (repeating group)
          name
          email
recipient list



  • Drag the message_subject and message_body fields onto the form to create text boxes for them, then drag the person repeating group onto the form and select 'Repeating Table'. Then, drag an expression box onto the screen and bind it to the recipient_list field, and finally, drag a button onto the form. I've set the button's label to 'Get Recipients'. Your whole form should now look like this:
















  • Next, lets edit the button's properties and create a new rule to execute when the button is clicked. From the Button Properties dialog, under the General tab hit the 'Rules' button. Hit the 'Add...' button to create a new Rule. Leave the Condition set to 'None - This rule always applies', and hit the 'Add Action...' button.
  • In the Action dialog, choose 'Set a fields value' from the 'Action' box, and use the button to set the 'Field' to update as the recipient_list field.














  • Now, hit the Fx button to supply a function, and type this in the function box:
     eval(eval(person, 'concat(my:email, ";")'), "..")
  • NOTE: You will need to highlight person, then using the 'Insert Field or Group' button, insert the person group. In the end, it should be underlined. The rest of the statement can be typed in manually.












  • Click Ok to get all the way back out to your form.
  • Now its time to test!
  • Hit the Preview button to preview the form.
  • Enter a name and email address in the first row of the table, then click the 'Insert item' link and add a second name and address. Then click the 'Get Recipients' button, and the email addresses will be concatenated together and separated by a semi-colon.



Part II of this post will focus on creating a Workflow in SharePoint designer to send your message.