Wednesday, July 16, 2008

Photo Gallery In SharePoint

Recently I had posted a query on how to build a photo gallery as a no code solution in SharePoint?

Here is the solution!!!

Functionality:
Develop a WebPart to display the photo gallery as shown in figure below:





Each of the Photos in the gallery will inturn be linked to some URL.

I used the Content Query WebPart to do that. And it is not tough at all!!!

Let me elaborate the steps:

1) Create the list to store the url for the photos and the url where it should be hyperlinked to :
Let’s call the list as PhotoList



Title: Stores the url where click on that particular photo should redirect to.
ImageURL: Stores the image path. (Image description can be used as alternate text and as tool tip on hover.)

2) Modify ItemStyle.xsl
a) Navigate to the style library on the site(Go to View All site content -> Style Library)
b) Download a copy of ItemStyle.xsl
c) Add the following Custom Style template for our PhotoGallery


<xsl:template name="PhotoGallery" match="Row[@Style='PhotoGallery']" mode="itemstyle">

<xsl:param name="CurPos" />

<xsl:param name="Last" />

<xsl:variable name="SafeLinkUrl">

<xsl:call-template name="OuterTemplate.GetSafeLink">

<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>

</xsl:call-template>

</xsl:variable>

<xsl:variable name="DisplayTitle">

<xsl:call-template name="OuterTemplate.GetTitle">

<xsl:with-param name="Title" select="@Title"/>

<xsl:with-param name="UrlColumnName" select="'LinkUrl'"/>

</xsl:call-template>

</xsl:variable>

<xsl:variable name="LinkTarget">

<xsl:if test="@OpenInNewWindow = 'True'" >_blank</xsl:if>

</xsl:variable>

<xsl:variable name="SafeImageURL">

<xsl:value-of select="@ImageURL" />

</xsl:variable>

<xsl:variable name="tableStart">

<xsl:if test="$CurPos = 1">

<![CDATA[<div id="tablediv" style="overflow-x:scroll;width=550px;"><table border="0" cellspacing="5"



style="margin:0pt;padding:0pt;width:100%;"><tr width="100%">]]>

</xsl:if>

</xsl:variable>

<xsl:variable name="tableEnd">

<xsl:if test="$CurPos = $Last">

<![CDATA[</tr></table></div>]]>

</xsl:if>

</xsl:variable>

<div id="linkitem" class="item">

<xsl:value-of select="$tableStart" disable-output-escaping="yes"/>

<xsl:if test="string-length($SafeImageURL) != 0">

<xsl:variable name="trimmedImageUrl">

<xsl:choose>

<xsl:when test="contains($SafeImageURL, ',')">

<xsl:value-of select="substring-before($SafeImageURL, ',')"/>

</xsl:when>

<xsl:otherwise>

<xsl:value-of select="@ImageURL"/>

</xsl:otherwise>

</xsl:choose>

</xsl:variable>

<td align="left">

<div class="image-area-left">

<a style="border:1px solid #9D0E2D;" href="{$DisplayTitle}" target="_blank"



title="{@LinkToolTip}">

<img class="image" height="100" src="{$trimmedImageUrl}" />

</a>

</div>

</td>

</xsl:if>



<xsl:value-of select="$tableEnd" disable-output-escaping="yes"/>

</div>

</xsl:template>









d) Save the changes and upload the ItemStyle.xsl in its original location. Check In the file.

3) Modify ContentQueryMain.xsl
a) Navigate to the style library on the site(Go to View All site content -> Style Library)
b) Download a copy of ContentQueryMain.xsl
c) Make the changes highlighted below.


Under <xsl:template name="OuterTemplate.Body">







<xsl:call-template name="OuterTemplate.CallItemTemplate">

<xsl:with-param name="CurPosition" select="$CurPosition" />

<xsl:with-param name="LastRow" select="$LastRow" />

</xsl:call-template>





















Under <xsl:template name="OuterTemplate.CallItemTemplate">







<xsl:param name="LastRow" />

<xsl:choose>


<xsl:when test="@Style='PhotoGallery'">

<xsl:apply-templates select="." mode="itemstyle">

<xsl:with-param name="CurPos" select="$CurPosition" />

<xsl:with-param name="Last" select="$LastRow" />

</xsl:apply-templates>

</xsl:when>








4) Add the content by query WebPart:

Add content by query WebPart and set the following properties for the WebPart:

a) Query -> Source -> Select Show Items from the following list -> browse to the PhotoList created in step1.

b) Query ->List type -> Show Items from this list type = Custom List

c) Query-> Content Type -> Show Items of this content type group = List Content type

d) Query -> Content Type -> Show Items of this content type = Item

e) Presentation -> Sort Items By = Created

f) Presentation -> Limit the number of items to display -> Item Limit : 100

g) Presentation -> Styles -> Group Style -> Default

h) Presentation -> Styles -> Item Style ->PhotoGallery








The WebPart would not display any data at this stage. It would work only after completing the step below.







5) Modify the Content Query WebPart to display the columns required :

a) Export the Content Editor WebPart and save the .WebPart file on desktop.

b) Open the file locate and modify the following line :







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





Add the column name as shown in bold to include that in the view. Save the changes

c) Upload the WebPart back to the site.



You should be able to get a photo gallery as shown at the top(Of course with an assumption that you have content in the PhotoList J)






Friday, June 13, 2008

The file manifest.xml does not exist in the solution package !!

While doing solution deployment was hit upon with an error message :
"The file manifest.xml does not exist in the solution package".



After several attempts to figure out what was going wrong, noticed that the package size was getting limited to 1400 KB irrespective of wether I removed or added several files.

Could overcome this by adding following lines (thanks to this ref) at the start of cab.ddf file.

.Set CabinetFileCountThreshold=0
.Set FolderFileCountThreshold=0
.Set FolderSizeThreshold=0
.Set MaxCabinetSize=0
.Set MaxDiskFileCount=0
.Set MaxDiskSize=0


This removes the file size limitations.

Special Characters in filename in .wsp file

Was bitten by a wierd issue while working on Solution deployment recently :

Error : Failed to extract the cab file in the solution

Some rounds of checks helped to locate the cause. One of the file packaged had "@" in the filename.

Hence be sure not to use any special characters in filename while getting the solution package ready.

Default RowLimit in SPQuery

Fumbled upon a strange behavior when using "SPQuery". Used SPQuery in one of the places and found that it is not returning all the results from the List queried !!!!


There was no way of debugging to locate the culprit until some SDK documentation came to rescue saying that the default resultset rowlimit for SPQuery is 100.

So make it a point to explicitly set it to some higher number :
query.RowLimit = 500;

When using "Lists" webService available in MOSS 2007, GetListItems has a parameter rowlimit which would allow to set this rowlimit.

Also when using SPQuery it bydefault would search only the current folder in the List. WSS v3.0 allows to search all subfolders in the same query using :
query.ViewAttributes = "Scope=\"Recursive\"";

This is especially important when querying Discussion Board list as it uses Folder hierarchy to store all the replies for a particular topic.

Getting an "There has been an error while processing the form." error while submitting a browser based InfoPath form :

While working on a Browser based InfoPath error was struck by an error message while submitting the form.


The intresting part was that the form was working perfectly when run from InfoPath client.


Something in the form was preventing it from getting submitted.Could not locate the culprit even after thorough recheck of logic.


Thanks to
this thread for helping me to find that the cause was a Secondary data connection
that was not getting used anywhere in the main datasource.

Error While trying to access Sharepoint List in subsite using Lists Webservice ?

I wanted to access the data from SharePoint List present on remote server in an utility. As the site was pesent on remote server, I used Lists WebService to access the data.


I added the WebReference as : http://mainsite/subsite/_vti_bin/Lists.asmx
and tried to run the code only to be hit with a Soap Exception.



Following are the error details I got :
System.Web.Services.Protocols.SoapException was unhandled by user code
Message="Exception of type 'Microsoft.SharePoint.SoapServer.SoapServerException' was thrown."
Source="System.Web.Services"
Actor=""
Lang=""
Node=""
Role=""
StackTrace:
at System.Web.Services.Protocols.SoapHttpClientProtocol.ReadResponse(SoapClientMessage message, WebResponse response, Stream responseStream, Boolean asyncCall)
at System.Web.Services.Protocols.SoapHttpClientProtocol.Invoke(String methodName, Object[] parameters)
at ProdLists.Lists.GetListItems(String listName, String viewName, XmlNode query, XmlNode viewFields, String rowLimit, XmlNode queryOptions, String webID) in c:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\utility\f0b09a93\a4ef1a67\App_WebReferences.3-c5ghn5.2.cs:line 496
at _Default.Page_Load(Object sender, EventArgs e) in d:\Projects\Utility\Default.aspx.cs:line 45
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Was unable to find the reason for this bahavior, however figured out that eventhough the reference was added to the WebService under subsite, it bydeault points to the Lists under mainsite or the top level site.


To tackle this I had to set the URL property expicitly at runtime.


Following are the lines you will have to add to be able to access the lists under any subsite :


ProdLists.Lists prodService = new ProdLists.Lists();
prodService.UseDefaultCredentials = true;
prodService.Url = "http://mainsite/subsite/_vti_bin/Lists.asmx";

Working with data from different SharePoint Fields

Many times we spend lots of time in small tasks like understanding the format in which SharePoint data is stored internally when using object model to retrieve/store data in SharePoint lists. So thought of getting all in one place.



Many of the field types like text, Number etc are straightforward to get/set.
A cleaner and professional way of working with the data from Fields would be to make use of Field Value Classes provided.

Following Field Value classes are available:
SPFieldURLValue
SPFieldLookupValue
SPFieldUserValue
SPFieldMultiChoiceValue
SPFieldRatingscaleValue
SPFieldMultiColumnValue


Ex:
1) To retrieve value from URL field :

SPListItem linkItem = SPContext.Current.Web.Lists["Links"].Items[0];
SPFieldUrlValue val = new SPFieldUrlValue(linkItem[SPBuiltInFieldId.URL].ToString());
string linkURL = val.Url;
string linkText = val.Description;

2) To Save value in URL field :

string linkURL = "http://www.samplesite.com/sampleweb";
string linkText = "SampleWebsite";
SPFieldUrlValue val = new SPFieldUrlValue();
val.Description = linkText;
val.Url = linkURL;
SPListItem linkItem = SPContext.Current.Web.Lists["Links"].Items.Add();
linkItem[SPBuiltInFieldId.URL] = val;
linkItem.Update();

Following are some more references on this :
http://spdevguy.com/blogs/spdev/archive/2007/11/16/reading-field-data-with-sharepoint-s-field-value-classes.aspx

http://msdn.microsoft.com/en-us/library/microsoft.sharepoint.spfieldurlvalue.aspx