onsdag den 28. marts 2012

JavaScript and Client Object Model - Get Latest Video Pages

SharePoint Apps



//initiates function SetLabelValues on page load:

ExecuteOrDelayUntilScriptLoaded(GetValues, "sp.js");

function GetValues()
{

//Get context - video site

var ctx = new SP.ClientContext("/videositename/");

//Get web:

var web = ctx.get_web();

//Get list

var videoList = web.get_lists().getByTitle('Pages');

//Get the latest video
var caml = new SP.CamlQuery();


this.allVideos = videoList.getItems(caml);

//Video is a custom site column
ctx.load(this.allVideos, "Include(Title,Video,Created, Comments,FileRef)");

//Video List
var enumerator = this.allVideos.getEnumerator();

while (enumerator.moveNext()) {

var li = enumerator.get_current();
var videourl = li.get_item("FileRef");
var videotitle=li.get_item("Title");
//build the str string here...
}

//Write list to a div tag with id="VideoList"

var div = document.getElementById("VideoList");
div.innerHTML = str;
}

lørdag den 14. januar 2012

dk.msn.com News style with Content Query

SharePoint Apps



Coding News web part that resembles News display at dk.msn.com (or uk.msn.com) with one top headline and 5 thumbnail pictures below was not that hard to code.
Here are the things that you have to code:
- display the latest item as headline with large picture
- display as thumbnails, both the latest item and the last 5-6 items below the headline news
- shuffle the items - display item no. 2 as headline and after some time, display item no. 3 and so on
- fade the thumbnail that is currently displayed as headline
- allow user to click or move the mouse over thumbnails and choose which news is displayed as headline
- allow user to click on the item and display the entire item on another page
Here are some tips:
1. Create a custom style in your custom ItemStyle.xsl that is referenced from Content Query web part
2. Create a variable RowNum to display the latest news item as top headline:
3. Write if statement to display only the first item:

..xsl:if test="$RowNum < 1">...

4. Define both a and img tag with id, so you can reference them from JavaScript:

id="newsdisplayimage" src="{$SafeImageUrl}" title="{@ImageUrlAltText}" />

5. Define onClick and onMouseOver for thumbnail image link:

javascript:ShowLarge('')

6. Define thumbnail image with id:



7. Create a reference to JavaScript on the page
8. ShowLarge JavaScript function - example of how to display the thumbnail image as headline - large image:

function ShowLarge(Row) { try {var image=document.getElementById(Row + "_image");var mainimage = document.getElementById("newsdisplayimage");
mainimage.filters.item("DXImageTransform.Microsoft.Fade").apply();
mainimage.src=image.src;
mainimage.filters.item("DXImageTransform.Microsoft.Fade").play();


9. Shuffle between news items:
var t=setInterval("ShowLarge()",12000);


10. Fade thumbnail image for the current headline:
image.style.filter="Alpha(opacity=50)";

mandag den 17. oktober 2011

SharePoint 2010 and How to Build XML and Display ListItem with XSLT

SharePoint Apps



In the following, I'll explain how to build XML on-the-fly and display data with XSLT. The advantage of this approach is that you always can edit the way data is displayed without changing the code in C# and deploying the new assembly.

So, if you are tired of using StringBuilder to display data in your web parts, check out the description below:

I assume that you know how to create a web part in VisualStudio and write the code to fetch SPListItem that I defined as "projectitem" in the code.

SPListItem projectitem=.....

You can define the path to XSLT file as a web part property that is editable in the web part ToolPane:

private string _xslt = "/Style Library/Projects/ProjectInfo.xsl";

[WebBrowsable(true),
Category("ProjectInfo settings"),
DefaultValue("Current"),
Personalizable(PersonalizationScope.Shared),
WebDisplayName("XSLT path"),
WebDescription("Pat to XSL file")]
public string XSLT
{
get { return _xslt; }
set { _xslt = value; }
}

Also, define the string that will contain the XML, built on the fly:

string xmlInfo = "";

The code is used to fetch the item from a "Project list" that contains metadata about projects. Whilst I also defined the list of ProjectFields as:

This list is used in the code to save all the list item values - the values, defined in the list item columns.
The code loops through the list item fields (columns), so I also defined the field name and field value as FieldName and FieldValue:


class ProjectFields { public string FieldName { get; set; } public string FieldValue { get; set; } }

I loop through the list item fields (columns):

foreach (SPField field in projectitem.Fields){}

Than, I fetch the value for each field (column). The code to fetch the field value is different for each field type, so

I write something like this:

if (field.Type == SPFieldType.Text){}

or

if (field.TypeAsString == "TaxonomyFieldType"){}

or

if (field.Type == SPFieldType.User){}

Don't forget to check for fields and empty field values:
if (projectitem[field.Title]!=null){}

and

if (projectitem[field.Title].ToString() != ""){}

At the end of the loop, the field name and field value are saved in the list:

projectList.Add(new ProjectFields { FieldName = field.Title.Replace(" ", "_"), FieldValue = itemvalue });

Than, the foreach loop is closed and I can build the XML on the fly:

XElement xml = new XElement("Project");
foreach (ProjectFields data in projectList)
{
xml.Add(new XAttribute(data.FieldName, data.FieldValue));

}

Than, save the XML to a string:
xmlInfo = xml.ToString();

Finally, render the XML with XSL:
protected override void RenderContents(HtmlTextWriter output)
{
RenderChildren(output);
try
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
if (xmlInfo.ToString() != "")
{
string rootWebUrl = SPContext.Current.Site.RootWeb.Url.ToString();

XslCompiledTransform xslt = new XslCompiledTransform();
XmlDocument loadXsl = new XmlDocument();
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultNetworkCredentials;

XsltSettings xslt_settings = new XsltSettings();
xslt_settings.EnableScript = true;
xslt.Load(rootWebUrl + XSLT, xslt_settings, resolver);


XmlReader xreader = XmlReader.Create(new System.IO.StringReader(xmlInfo.ToString()));
System.Xml.XPath.XPathDocument pData = new System.Xml.XPath.XPathDocument(xreader);

xslt.Transform(pData, null, output);

}
});
} .......end with catch....


In VisualStudio, add a new module to your project - I called it XSLT and changed the Elements.xml file so it looks like this:

Than, add a new XSL file and call it ProjectInfo.xsl. Here is an example of how to display a field value called

ProjectManager in .xsl file:





NOTICE: DELETE ALL THE br TAGS FROM THE CODE

torsdag den 31. marts 2011

SP.CamlQuery & HTML web part

How to display list content using HTML web part?
1. Add HTML Form Web Part to the page in SharePoint 2010
2. Click on the arrow and choose Edit Web Part
3. In the Tool Pane, click on Source Editor button
4. Add the following code (delete all br />):

tirsdag den 14. december 2010

70-573: Top 5 answers

1. Client object model: .ExecuteQuery() is missing somewhere after Load method
2. Memory leaks, bad performance: site.Dispose() is missing
3. Permissions, sandboxed solution: RunWithElevatedPrivileges is either missing or not supported
4. Not the right answer: Changes in Web.config, overwrite standard SharePoint files, and modify registry
5. Always the right answer: change Onet.xml

søndag den 12. december 2010

Upgrade to SharePoint 2010 – top 5 hurdles

1. System requirements: Minimum 4GB RAM, 64-bit Windows Server 2008 with SP2, 64-bit SQL Server 2005 with SP2 or later or SQL Server 2008.
2. Customizations: Just about everything, from master pages to MySites or RadEditor is customized, and you are afraid that it can’t be easily upgraded.
3. Social features, the ribbon etc.: Change even for the better requires additional resources and time to learn about the new platform.
4. People and technology issues: Who’s to blame if it doesn’t work; you, your vendor, or Microsoft? Is there an idea to wait for the official service pack 1?
5. Business value: Do benefits outweigh the cost of upgrade?

lørdag den 11. december 2010

Top 5 things I like about SharePoint

Besides being a corporate news channel and storage for documents, can you name 5 things SharePoint is used for in your organization?

Let me help you with top 5 things I like about SharePoint:
1. Document management: documents and pages based on document/page templates and content types, and metadata driven navigation and search
2. User Profiles: clearly defined and easy to find user skills, responsibilities, and activities
3. Lists: more effective and flexible work with information held and displayed as calendars, surveys, car rental, tasks, Q&A etc.
4. Role based content: metadata (skills, department etc.) in your user profile defines the content that you see; news, documents, pages, and lists
5. Web 2.0: Even the corporate news can be a two way communication with comments and rating that allows you to contribute with your knowledge and opinion