Zieglers

Just little about C#, .NET, SQL Server, SharePoint and SAP

Posts Tagged ‘spservices’

SPNotifications published on CodePlex!

Posted by zieglers on August 29, 2011

Finally today, I was able to publish my latest CodePlex project – SPNotifications.

SPNotifications provides floating warning/alert type of notification boxes which can be used to notify your SharePoint users.

Using SPNotifications, you can notify your users by show notification messages such as weather warnings, corporate updates, events …etc.
Notifications can be displayed on any page of your selection, for example, home page of your intranet, or landing pages for each department.

SPNotifications uses jQuery – jGrowl plugin for displaying notifications. Your notifications are stored in a custom list called ‘Notifications’.
Power users or admins can also use a custom application page to populate new notification messages.

You can find all the details on SPNotifications site on CodePlex.

Please give it a try and let me know what you think..

zieglers

Posted in IT Stuff, SharePoint, SharePoint 2010 | Tagged: , , , , , , , , , | Leave a Comment »

jGrowl Notifications for SharePoint 2010 – Part 2

Posted by zieglers on August 22, 2011

This article is the second part of “jGrowl Notifications for SharePoint 2010” series. If you haven’t read the first part of the series, you can read it here.

In the first article, we implemented the infrastructure for “jGrowl Notifications for SharePoint 2010”. From now on, i’ll refer to it as SPNotifications. First version was pretty raw in the sense that it was still using soap envelopes which are really hard to construct and not that practical to use. Also first version was just a teaser and it was missing some business logic too.

What’s in this article?

In this article i’ll try to show how to replace AJAX calls using soap envelopes with SPServices calls.

SPServices is a jQuery library which abstracts SharePoint’s Web Services and makes them easier to use. It also includes functions which use the various Web Service operations to provide more useful (and cool) capabilities. It works entirely client side and requires no server install.”

Once we have a cleaner syntax, we’ll add a simple logic where notifications will be read from a custom list called -you guessed it- Notifications. Also upon closing a notification, we’ll implement a close callback logic which will mark notification as read. That should do it as for the scope of this second article. Further improvements will be explained in future articles of the series.

Implementation

First of all, let’s try to list implementation tasks:

  1. Include SPServices default syntax.
  2. Generate CAML query for AJAX call.
  3. Implement completeFunc details.
  4. Implement MarkAsRead function.
  5. Test it!!!

1. Include SPServices

Inside document.ready include this SPServices call as shown below.

// ***************************************

$().SPServices({
operation: “GetListItems“,
async: false,
listName: “Notifications“,
CAMLQuery: query,
CAMLViewFields: fields,
completefunc: function (xData, status){

} // end – completefunc

}); // end – SPServices

// ***************************************

Operation is GetListItems – since we want to read items from Notifications list.

listName is Notifications – a custom list with additional fields Description, Assigned To and Status (Read, Unread).

Choice values for Status field are ‘Read’ and ‘Unread’. Also, at this point you can add some sample items in Notifications list.

2. Generate CAML Query

Now, we have the skeleton for SPServices call, we can go ahead and specify our CAML query. We want to show only notifications where:

  • Assigned To values is “current user”
  • Status is different from ‘Read’

query‘ variable will store CAML query to be executed. ‘fields‘ variable will have fields that we want to display in the notification.

// ***************************************

var query = “<Query><Where><And><Eq><FieldRef Name=’AssignedTo’ /><Value Type=’Integer’><UserID Type=’Integer’ /></Value></Eq><Neq><FieldRef Name=’Status’ /> <Value Type=’Choice’>Read</Value> </Neq> </And> </Where> </Query>”;
var fields = “<ViewFields><FieldRef Name=’Title’ /><FieldRef Name=’Description’ /></ViewFields>”;

// ***************************************

You can include these variables at the top of the script as shown below.

3. Implement completefunc

This is the part that now we have our results returned and ready to be formatted before displayed. As mentioned in first article too, jGrowl will be used for presentation of notification messages. Here we will be setting some jGrowl options.

Position:

Life:

Sticky:

Close:

Let’s set position and life params at the beginning of completefunc.

Next, we can start looping through each row returned and display them in the format we want. All data returned will be stored in xData.responseXML object.

// ***************************************

completefunc: function (xData, status){

$.jGrowl.defaults.position = “bottom-right”;
$.jGrowl.defaults.life = 10000;

$(xData.responseXML).find(“z\\:row”).each(function() {

// Format your row here…

}); // end – each function

} // end – completefunc

// ***************************************

Here, let’s stop for a minute and think of how to implement a header part of the notification messages saying “Your messages for today” for instance. For that i introduce a flag called firstMessage, which will help us to construct header message by executing the code only once as implemented below. This header has a lifespan of 5 seconds.

Next we start formatting notification messages for each entry in Notifications list. First let’s get notification ID – SPListItem ID – so that we can provide a link to notification. In messageHtml variable, we basically construct the html part for each notification.

Layout is an icon for the notification followed by its title – linked to notification list item – and below its description.

4. Implement “Mark as Read” logic

Almost done for this article! One thing left, which is marking notifications as read upon clicking close button, so that they are not displayed each time page is loaded. This logic is definitely a must. Imagine having SPNotifications for your intranet and assume that notifications are displayed when you visit homepage of your intranet – or any other department landing page. In that case, showing same notifications every time you visit the home page wouldn’t be that nice, as some point it’d get annoying!! Since this is the case, we need to implement a ‘close callback’ logic here to mark notifications as read.

jGrowl has already an option for close callback – as mentioned in section 2 above. So all we have to do is to implement it in a way that notification list item is updated using SPServices UpdateListItems operation.

Here is our MarkAsRead function which only takes notification ID as input parameter. I’m capturing all script structure so you can see where this function sits. Basically it’s right out of document-ready function scope, at the global level of entire javascript.

Now we have the ‘MarkAsRead’ function ready, all we have to do is to add the code snippet to call it. You can include this right below where we constructed the html for notification messages as shown below.

…and we are done!!! it’s time to test it now..

5. Test your first SPNotifications

To test it, drop a CEWP to your homepage and provide the link SPNotifications script in CEWP options. For example, you can upload the script to Shared Documents, just for testing purposes.

In my Notifications list, i created 3 entries having them assigned to myself and their status being ‘Unread’. Visiting my homepage, I see the following.

After 5 seconds, notifications header ‘Your messages for today’ will disappear. Note that in jGrowl options, I set sticky to true.

Now let’s say, I read the ‘Parking Lot Closed’ notification and clicked that little ‘X’ button. This will trigger close callback function and status for that notification will be set to ‘Read’, i.e. that notification will be marked as read. You can check it from Notifications list as shown below.

Now, let’s visit our homepage again and see only 2 unread notifications are left instead of 3 notifications.

That concludes this article – jGrowl Notifications for SharePoint 2010 – Part 2.

Now I have to think a better way to store and populate those notifications from power user or admin perspective.

Thanks for reading. If you have any suggestions, feedback, having trouble to implement; feel free to drop a note.

 

zieglers

Posted in IT Stuff, SharePoint, SharePoint 2010 | Tagged: , , , , , , , | 2 Comments »

Dynamic jQuery Tooltips for SharePoint

Posted by zieglers on November 10, 2010

In this post, I’ll try to show how you can implement tooltips with dynamic content for any link in SharePoint pages.

 

Ingredients

  • jquery.1.4.3.min.js
  • jquery.simpletip-1.3.1.min.js
  • jquery.SPServices-0.5.7.min.js
  • 1 teaspoon custom tooltip style
  • 1 CEWP

 

Preparation

 

Add jQuery references

 

Firstly, add a CEWP to your site home page. Then edit your CEWP in Source Editor and add the following references with low heat.

<script type=”text/javascript” language=”javascript” src=”https://ajax.googleapis.com/ajax/libs/jquery/1.4.3/jquery.min.js”></script&gt;

<script language=”javascript” type=”text/javascript” src=”Shared%20Documents/jquery.SPServices-0.5.7.min.js”></script>

<script type=”text/javascript” language=”javascript” src=”Shared%20Documents/jquery.simpletip-1.3.1.min.js”></script>

Add jQuery libraries

While references are getting warmer, add below files in your Shared Documents pot.

 jquery.1.4.3.min.js

http://code.google.com/apis/libraries/devguide.html#jquery

jquery.simpletip-1.3.1.min.js

http://craigsworks.com/projects/simpletip/

jquery.SPServices-0.5.7.min.js

http://spservices.codeplex.com/

Get code structure ready

Secondly, add your javascript code skeleton to your CEWP.

<script type=”text/javascript”> 

$(document).ready(function() {

   // Code goes here

   }); // end – document ready

</script>

Ok, so far our preparation is done. Now we can start cooking our meal.


Call SharePoint Web Services using jQuery

Since I chose ‘list permissions’ as for our dynamic content flavor for our tooltip, first we need to call GetPermissionCollection method of permissions.asmx web service. For a given list, this call will return us all the taste we are used to getting from a list permissions page such as the one below.

 

Call permissions web service

Add following code to your document.ready pot.

// Call permissions web service

  $().SPServices({

    url: “/_vti_bin/permissions.asmx”,

    operation: “GetPermissionCollection”,

    async: false,

    objectName: “Tasks”,

    objectType: “List”,

    completefunc: function (xData, Status) {

           // Format your output here

       } // end – completefunc

  }); // end – SPServices

Once web service is successfully invoked, the result will be returned in xData. Then, in completefunc method, you can modify, format, display your result as you wish.

In this case, I choose to feed my tooltip with the result of my web service call. This will allow me to show list permissions, once user hovers mouse over a list in quick launch menu.

Select fields you want to display

But before I do so, I need to select which part of result I need to show in my tooltip. Here, I choose to show only users and groups which have permissions on that list.

Add following code to your completefunc pot.

            // Loop thru each permission

            // Display users and groups

            //$(xData.responseXML).find(“Permission”).each(function() {

            $(xData.responseXML).find(“Permission”).each(function() {

                 var xmlGroupName = $(this).attr(“GroupName”);

                 var strGroupName = String(xmlGroupName);

                 var xmlUserLogin = $(this).attr(“UserLogin”);

                 var strUserLogin = String(xmlUserLogin );

                 if (strGroupName != ‘undefined’)

                     $(“#callResponse”).append(“<li>” + strGroupName + “</li>”);

                 else

                     $(“#callResponse”).append(“<li>” + strUserLogin  + “</li>”);

            });

In above code you’ll see that group and user names are appended to a div element, whose id is callResponse. So, let’s add that div too in our CEWP. You can put anywhere outside of <script> tags.

Add Tooltip to SharePoint links

 

We are done with heavy lifting, now it is show-off time. Our meal has been cooked to perfection and ready to be served now. One last thing to do: get tooltip plate ready.

I chose Tasks link in quick launch menu as my victim. I’ll add my tooltip to Tasks link so that when users mouse over that link, they’ll see my delicious list permissions content in a beautifully served tooltip plate.

Add following code after the end of SPServices line.

    // Tooltip

    $(‘a[href*=”Tasks”]’).simpletip({

       content: $(“#callResponse”),

       offset: [50, 0]

    }); // end – Tooltip


Add Tooltip Style

Finally, before we get our plate in hand and walk out of kitchen door, a final touch-up is required to wow our guests. That one is our humble inline style below. Of course if you wish, you can put that style in your some css files later on.

<STYLE>

.tooltip{

  position: absolute;

  width: 250px;

  padding: 8px 10px;

  background-color: #F2F2F2;

  border: 2px solid #848484;

  cursor: pointer;

  text-decoration:none;

}

</STYLE>

Conclusion

Now you walk out of SharePoint development kitchen door and put your masterpiece in front of your guests and enjoy watching them when they are eating.

Bon Appetit!!!

zieglers

Posted in IT Stuff, SharePoint | Tagged: , , , , , , , | 1 Comment »