Showing posts with label web part. Show all posts
Showing posts with label web part. Show all posts

Wednesday, August 18, 2010

Display Silverlight in SharePoint Content Editor Webpart


The steps:
  1. Created a web part page document library
  2. Uploaded xap into the library
  3. Create a new web part page in the library
  4. Added a content editor web part to the page.
  5. Added an object tag pointing to the XAP file in the source code of the content editor web part

And, voila.... nothing displayed! Played with adding the Silverlight.js file to the library, and various other techniques. Finally found the key was the interaction of the object tag width and height with the rendered dimensions of the web part.

Solved by either:

  • Setting a fixed height and width in the web part appearance properties, and giving the object tag a height and width of 100%, OR
  • Setting an absolute height and width for the object tag and leaving the web part with non-fixed height and width

Monday, April 12, 2010

SPTreeView Expand/CollapseAll No Postback

SPTreeView has the functions ExpandAll() and CollapseAll(). Usually one would insert image buttons with onclick event handlers that calls the 2 functions above. The downside of this is that the image buttons will cause a postback, and the entire page will reload. This is not an ideal situation, especially if the tree view is a web part within the SharePoint page.

Here's how we can achieve the same objectives without a postback (using Javascript):


JS Functions to handle Expand/Collapse of nodes

function TreeView_ExpandCollapseAll(data, id, lineType, flag) {

if (!data) { return; }

$("[id='" + id + "'] a[id^='" + id + "n']").each(function() {

nodeID = $(this).attr('id');
var idx = nodeID.substring(id.length + 1);
TreeView_ToggleNode(data, idx, $(this).context, lineType, document.getElementById(nodeID + "Nodes"), flag);

});

}

function TreeView_ToggleNode(data, index, node, lineType, children, flag) {

if (!data) { return; }

var img = node.childNodes[0];
var newExpandState;

try {

if (flag == 1) {

children.style.display = "block";
newExpandState = "e";

if ((typeof (img) != "undefined") && (img != null)) {

if (lineType == "l") { img.src = data.images[15]; }
else
if (lineType == "t") { img.src = data.images[12]; }
else
if (lineType == "-") { img.src = data.images[18]; }
else { img.src = data.images[5]; }

img.alt = data.collapseToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));

}

}
else {

children.style.display = "none";
newExpandState = "c";
if ((typeof (img) != "undefined") && (img != null)) {

if (lineType == "l") { img.src = data.images[14]; }
else
if (lineType == "t") { img.src = data.images[11]; }
else
if (lineType == "-") { img.src = data.images[17]; }
else { img.src = data.images[4]; }
img.alt = data.expandToolTip.replace(/\{0\}/, TreeView_GetNodeText(node));

}

}

} catch (e) { }

data.expandState.value = data.expandState.value.substring(0, index) + newExpandState + data.expandState.value.slice(index + 1);

}


The function TreeView_ToggleNode exists in a default JS file that is auto-embedded when you use the SPTreeView class (WebResource.axd?...). We are modifying it to take in an additional flag parameter to control whether to expand or collapse the nodes.

Include reference to JS file from WebPart code

Literal ltlJs = new
Literal();
ltlJs.Text = "<script type='text/javascript' src='http://domain-name/Assets/js/TreeView.js'></script>";
this.Page.Header.Controls.Add(ltlJs);


Call Javascript function from WebPart controls

HtmlImage TreeExpandAll = new
HtmlImage();
TreeExpandAll.Src = strExpandTree;
TreeExpandAll.Alt = "Expand";
TreeExpandAll.Attributes["onclick"] = "TreeView_ExpandCollapseAll(" + treeViewSPW.ClientID + "_Data, '" + treeViewSPW.ClientID + "',' ', 1)";


HtmlImage TreeCollapseAll = new
HtmlImage();
TreeCollapseAll.Src = strCollapseTree;
TreeCollapseAll.Alt = "Collapse";
TreeCollapseAll.Attributes["onclick"] = "TreeView_ExpandCollapseAll(" + treeViewSPW.ClientID + "_Data, '" + treeViewSPW.ClientID + "',' ', 0)";



Note that the object treeViewSPW must be added to this.Controls before treeViewSPW.ClientID can be used.

Monday, March 29, 2010

Getting SharePoint subwebs

You may encounter access denied problems when you try to run the code web.Webs using a non-admin account. This problem may occur even when the user account has access to all the subsite of the particular web. The entire page in which the webpart is in becomes "Access Denied" although only the webpart is affected.

SPSecurity.CatchAccessDeniedException = false; will allow the user to access the page, but the webpart would display an "Unauthorised Access Exception".

A more accurate function would be web.GetSubwebsForCurrentUser()

Thursday, August 27, 2009

Deploying a SharePoint webpart

There are several ways to do it:

From Visual Studio 2008 [Condition: You have the source code]
1) In Visual Studio 2008, DEPLOY webpart solution
2) Go to local SharePoint, Site Actions -> Site Settings
3) Under the Site Collection Administration header, click "Go to top level site settings"
4) Under Galleries, click "Web Parts". You should see your web part listed there.
5) Under Site Collection Administration, click "Site collection features". Your web part should be Activated.
6) Add a new web part page, click Edit Page and then add the web part to the page.

Using CMD [Condition: You only have the wsp file]
1) Type cd C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\12\BIN
2) Then type stsadm -o addsolution -filename [insert filename]
This will add your solution (.wsp) into SharePoint
3) Launch SharePoint Central Administration.
4) Under Operations, click on Solution Management. You should see your webpart there.
5) Click on the web part, then click Deploy.