天天看點

Global Announcements Webpart – New Code for free.

The Announcements list is a default item in Team Sites.  The webparts that are generated with them are nice to have out of the box, but they have one limitation that has always bothered me – they are only at a site level.  In order to get Global Announcements, you’d have to do one of two things: create an Event on the Announcements lists and have them propagate to all their sub lists, or write a webpart that gets all the Announcements Lists.  Me, I did the latter.

My goal was to build a webpart that would look up the site hierarchy and get all the entries from the Announcements lists from all the site’s parents.  This will create a list that will show only the relevant Announcements since sister sites should not relate to the current site, but parent sites will.  For example, imagine a root site that has an announcements list meant for the entire company.  Below the root sire are the HR and Accounting sites.  They too have announcements, but they only post announcements for their depts.  Below Accounting are the Billing and Accounts Payable departments.  They too have local announcements.  By adding the Global Announcements webpart to the Billing webpage, users of the site will see Announcements from Billing, Accounting, and the Root site.  Since Announcements from Accounts Payable and HR do not directly apply to Billing (and are not in the direct path from Billing to the root), they are removed from the global list.

How it works.

The webpart makes one assumption: that the Announcements list is the out of the box Announcements List – unchanged in any way.  If you want to adjust for differences, I’ve included the code in the download. 

One key problem from just iterating through the site hierarchy was sorting the announcements so they were in proper order.  Not wanting to sort them manually, I turned to the SPSiteDataQuery.  This allows for a CAML query to be placed over an entire site collection or, as how I used it, a specific number of lists.  To do this, the SPSiteDataQuery is divided up into four section: the Lists, the CAML, the Web Scope, and the ViewFields. 

The Lists is fairly straightforward.  It begins (and ends) with a <Lists> (</Lists>) tag.  Here is where you’ll place the specific lists to query.  You can also have it search all the lists in the SiteCollection by leaving it blank.  But in this example, we’ll specify lists.  To do this, you need to get the ID for the lists and place them in List tags.  The item will look like this:

<List ID=”LIST-GUID-GOES-HERE” />

From there, we fill the Query.  This is straightforward CAML to give selection criteria for the lists.  I have it to filter out entries that have expired.  I won’t go oever the CAML since the query is in the code and teaching CAML isn’t in the scope of this discussion. 

Finally, the ViewFields need to be addressed.  Much like the SELECT command in a SQL query, this will choose the fields to make available for use/display.  Here a series of <FieldRef Name=”Title” /> tags (replacing “Title” with the field name you want displayed) tell the query what to return.

One nice addition to this query is the RowLimit property.  You can tell the query how many items to return.  This is helpful in order to limit the number of items displayed.  I’d rather let SharePoint do my work for me than write code to decide how many records have been returned.

From here, you call the GetSiteData(query) function in the SPContext.Current.Web object to get a DataTable of the items you requested.  For this code, it’s the top few announcements from the sites above the current site. 

Some Problems

Of course, getting list items in Anonymous sites can cause a problem.  So it will need special security file (CAS) for non-authenticated sites.  Also, if you don’t have access to a site, the CatchAccessDeniedException setting is set to false when accessing the site.  This and a try/catch will let the code skip any Announcements that the user doesn’t have permission to view (or for those sites where the Announcements list has been removed. 

Additions

The OOtB Announcements part offers up the Author in addition to the announcement body and the title.  I have included those and added another item.  Now, each announcement will show what site it originated at.  This will help the user understand what context the announcement was made in.  This feature (as well as the Author display) can be turned off in the WebPart Settings.

One deletion is the “Add New Announcement” link.  I will add this later.  Part of the problem with this link was, to me, where does one put it.  And since my initial scope of the webpart was to display announcement and not add them, I left it at a display only.  I’ll let you know when I add this feature in.

The Code

Enjoy it.  I put the code out there in case you wanted to make adjustments to it.  It’s in my downloads section.  If you use it, I hope it suits your needs.  If you change it, please leave my name in it.  I like credit.  Credit is nice.  But the usual disclaimers apply.  Use at your own risk.  And this is another reason why I am including the code.  I like knowing what’s on my farm.  I think you should have the ability to know as well.  This way, you can see for yourself by seeing the code. 

One more thing.  If you make any improvements/changes, please let me know.  I would love to see how the webpart has been made better.  The last time I gave code out, another user on the site (thanks Chris) improved my code and made it easier for people to use. 

Enjoy.  I know it's not much, but I hope you find it somewhat useful.

轉至:

http://www.thesug.org/blogs/lsuslinky/Lists/Posts/Post.aspx?List=ee6ea231%2D5770%2D4c2d%2Da99c%2Dc7c6e5fec1a7&ID=20

繼續閱讀