How to Using a ModalPopupExtender in a SharePoint Webpart Class

How to Using a ModalPopupExtender in a SharePoint Webpart Class

Almost all of the examples on how to use the Ajax Control Toolkit ModalPopupExtender are done in ASP.Net, however in this example I’ll explain how to use this control in a SharePoint Webpart with all of the code living in the class.

For our purpose here this example assumes that you have already intergrated the Ajax Control Toolkit with your SharePoint environment.

First create the target control which is going to be used to show the modal view. In my example I made the button invisible because I plan to show the modal on page load. If you haven’t already done so, also add the System.Web.Extensions reference to your project:

Button btn = new Button();
btn.Attributes.Add(“style”, “display: none”);
btn.Text = “Show Modal”;
btn.ID = “btn”;

Next, create you ModalPopupExtender

AjaxControlToolkit.ModalPopupExtender mp = newAjaxControlToolkit.ModalPopupExtender();
mp.CancelControlID = “CancelBTN”;
mp.OkControlID = “OkBTN”;
mp.PopupControlID = “ModalPanel”;
mp.PopupDragHandleControlID = “ModalHeader”;
mp.BackgroundCssClass = “ModalPopupBG”;
mp.TargetControlID = “btn”;

Now create the main panel for your modal view as well as the DIV’s for the main body content

Panel pan = new Panel();
pan.Attributes[“display”] = “none”;
pan.Attributes[“id”] = “ModalPanel”;

HtmlGenericControlDivMain = new HtmlGenericControl(“div”);
DivMain.Attributes[“class”] = “Container”;

HtmlGenericControl div1 = new HtmlGenericControl(“div”);
div1.Attributes[“id”] = “PopupHeader”;
div1.Attributes[“class”] = “PopupHeader”;
LiteralHeaderText = new Literal();
HeaderText.Text = “<p>ALERT</p>”;
div1.Controls.Add(HeaderText);

HtmlGenericControl div2 = new HtmlGenericControl(“div”);
div2.Attributes[“id”] = “PopupBody”;
div2.Attributes[“class”] = “PopupBody”;
LiteralBodyText = new Literal();
BodyText.Text = “<p>This is a simple modal dialog</p>”;
div2.Controls.Add(BodyText);

HtmlGenericControl div3 = new HtmlGenericControl(“div”);
div3.Attributes[“id”] = “PopupBody”;
div3.Attributes[“class”] = “PopupBody”;
LiteralControlText = new Literal();
ControlText.Text = “<input id=’ReviewBTN’ type=’button’ value=’Okay’ /><input id=’DismissBTN’
type=’button’ value=’Dismiss’ />”;
div3.Controls.Add(ControlText);

DivMain.Controls.Add(div1);
DivMain.Controls.Add(div2);
DivMain.Controls.Add(div3);
pan.Controls.Add(DivMain);

Now add your controls to the webpart

this.Controls.Add(btn);
this.Controls.Add(pan);
this.Controls.Add(mp);

// I execute this on load, however it can be applied to an action on the target control button
mp.Show();

Here are the styles I used for the modal

.ModalPopupBG
{
background-color: #666699;
filter: alpha(opacity=50);
opacity: 0.7;
}

.PopupHeader
{
background-color: #5e8cc5;
padding: 5px 15x 5px 15px;
font-family: Arial;
font-weight: bold;
font-size: 12px;
color: #FFFFFF;
padding: 1px;
}

.PopupBody
{
padding: 5px 15px 5px 15px;
font-family: Arial;
font-weight: bold;
font-size: 12px;
color: #000000;
clear: both;
}

.Container
{
background-color: #ebf4fe;
border: 2px solid #000000;
padding: 0px 0px 0px 0px;
}

Read More

Create A Simple Org Chart Using SharePoint, PeopleSoft and Google Visualizations

Create A Simple Org Chart Using SharePoint, PeopleSoft and Google Visualizations

If your organization is anything like mine, you have a collection of Org Charts from every department.  Most of them are formatted differently and all of them get out-of-date pretty quickly.  Sure, there are tons of third party solutions, but they all assume that you:

a. Keep all of your organizational information in Active Directory or

b. Don’t mind duplicating (and maintaining) your organizational information in yet another system.

Well, neither of those was true for us.

What I plan to show in this post is a simple approach to create a completely dynamic Org Chart using SharePoint, PeopleSoft, and Google Visualization.  If you don’t use PeopleSoft, don’t worry, these can be EASILY modified to use your data source of choice.

What You Need:

  1. Access to a view of the PeopleSoft data that contains the ID of each employee and that of their manager.
  2. SharePoint Designer
  3. Ability to create a Stored Procedure.
  4. Basic knowledge of XSL, Javascript, and CSS won’t hurt.

Steps:

  1. Create a stored procedure to recursively retrieve managers and their staff, accepting a parameter for the Employee ID of the top level manager you are looking for and Level <= 2 means give me 2 levels below the top level manager:
    WITH DirectReports (Name, ReportsTo, Position, JobTitle, EmployeeID, Level)
    AS
    (
         SELECT
           rtw.Name,
           rtw.ReportsTo,
           rtw.position,
           rtw.JobTitle,
           rtw.EmployeeID,
           0 AS Level
        FROM [PeopleSoft].[WhoReportsToWhoView] AS rtw
        WHERE EmployeeID = @EmployeeID 
    
    UNION ALL 
    
        SELECT
           rtw.Name,
           rtw.ReportsTo,
           rtw.Position,
           rtw.JobTitle,
           rtw.EmployeeID,
           Level + 1
        FROM [PeopleSoft].[WhoReportsToWhoView] AS rtw
    
        INNER JOIN DirectReports AS d ON rtw.ReportsTo = d.Position
    )
    Select
       dr.Name,
       dr.JobTitle,
       dr.EmployeeID,
       dr2.EmployeeID as ReportsTo
    FROM DirectReports as dr
    LEFT OUTER JOIN
    DirectReports as dr2 ON dr.ReportsTo = dr2.Position Where dr.Level <= 2
  2. From SharePoint Designer create a SQL DataSource for the Stored Procedure above, passing in the EmployeeID paramter as a querystring parameter. You should be able to Google that, it is pretty well documented.  Sorry, I can’t hold your hand forever.
  3. Download this XSLT file and add it to your SharePoint Style Library:
  4. From SharePoint Designer add a Data View WebPart to the page. Set the XSLT path to that of the newly uploaded file.
  5. Viola! You should end up with something similar to the image below.  Just click on any employee to drill down into his/her organization.

To learn more about the inner-workings and various options of Google Visualization, check out Google’s documentation.

Read More

How to Embedding a PDF Object in a SharePoint 2010 Web part using C#

How to Embedding a PDF Object in a SharePoint 2010 Web part using C#

Recently I was assigned the task of displaying a PDF in a SharePoint Web part. I figured it’d be easy enough to display it in an <iframe> or simply embed it on the page, but unfortunately my assumption couldn’t have been further from the truth.

Attempting to load the PDF in an <iframe>

Quite simply, and by default, SharePoint does not allow users to automatically open documents within a browser window. There is a setting on the web application called Browser File Handling, which if set to Permissive is supposed to allow documents to open within the browser. Alas, this setting does not afford the ability to view PDFs within the browser window. Also, within the Document Library, there’s a setting called Opening Documents in the Browser, which defaults to opening files within the browser anyway… So SharePoint is working against us.

Using the <embed /> tag

The <embed /> tag was deprecated for XHTML in favor of the <object> tag, so it’s not “supported” in Visual Studio. There is however a way around this; create a Literal control in C# and then write it to your page at run-time to avoid any compile errors. Upon assigning the src attribute, this strategy proves fruitful as it simply embeds the PDF in the window as desired. This is great, except for the fact that this strategy is completely invalid. I wanted to do one better.

Using the <object> tag

The <object> tag is a not only a valid XHTML control; it’s also supported in Visual Studio. Unfortunately, it requires a Class, ClassID, or ProgID attribute. I wasted hours scouring the web for this specific ClassID, and in the end, I could not locate it. This drove me to create a new strategy; a hybrid of the <embed /> and <object> strategies. This way I can ensure maximum browser support while doing my best to implement valid code.

//Create your URL to the pdf file, including path and URL variables
string url = web.Url + “/” + “DocumentLibrary” + “/” + CurrentMonday.ToString(“MM-dd-yyyy”) + “.pdf” +
“#scrollbar=0&statusbar=0&navpanes=0&toolbar=1&zoom=75″;
//Establish Literal control
Literal l1 = new Literal();
//Write the Object code to the Literal
l1.Text = “<object id=’AcrobatFrame’ type=’application/pdf’ classid=’clsid:CA8A9780-280D-11CF-A24D-444553540000′ width=’711′ height=’956′><param name=’src’ value=’” + url + “‘ /><a href=’” + url + “‘>Click here to download “ + CurrentMonday.ToString(“MM-dd-yyyy”) + “.pdf</a></object>”;
//Add the Literal to the Page
Panel.Controls.Add(l1);

Acrobat URL variables

In the code above you can see that I am passing URL variables to define what application components are visible and the zoom of the pdf. This way I can set a static height and width for the object because I know the size of the document will always be the same. See a list of all URL variables.

 

Read More

How to Customize the background of a UITabBarController

How to Customize the background of a UITabBarController

Customizing the background of a UITabBarController

Here is a common question I get and I see this question a lot on the forums “how do I change the background of a UITabBarController?”. Well, it’s really quite easy, and it can make a big difference on how your iPhone application is presented.

Step 1

In order to achieve the above you are going to need to create a custom UITabBarController class. In XCode, choose ‘New File’ and then select ‘Cocoa Touch Class/Objective-C Class (subclass NSObject)’. Call the class ‘CustomUITabBarController’.

Open up CustomUITabBarController.h and ensure the code looks like the following:

#import <UIKit/UIKit.h>

@interface CustomUITabBarController: UITabBarController {
IBOutlet UITabBar *tabBar1;
}

@property(nonatomic, retain) UITabBar *tabBar1;
@end

Open up CustomUITabBarController.m and ensure the code looks like the following:

#import “CustomUITabBarController.h”
@implementation CustomUITabBarController
@synthesize tabBar1;

– (void)viewDidLoad {
[super viewDidLoad];
tabBar1.backgroundColor = [UIColor clearColor];
CGRect frame = CGRectMake(0, 0, 480, 49);
UIView *v = [[UIView alloc] initWithFrame:frame];
UIImage *i = [UIImage imageNamed:@”grad.png”];
UIColor *c = [[UIColor alloc] initWithPatternImage:i];
v.backgroundColor = c;
[c release];
[[self tabBar] insertSubview:v atIndex:0];
[v release];
}

@end

Step 2

Open up the ‘MainWindow.xib’ and choose the Tab Bar Controller. Now look at the property inspector and change the class to your custom class, then associate the tabBar1 outlet to the Tab Bar Controller.

You may have noticed from the code that it references a file called ‘grad.png’. This is the new background image for your tab bar. In my example the image is 320px x 49px, however you may want to double this for the retina display.

You should now have a custom UITabBarController background! What do you think?

Read More

Using SP.UI.ModalDialog in SharePoint 2010

Using SP.UI.ModalDialog in SharePoint 2010

If you are looking for primarily ‘client side’ functionality, you can use the SP.UI.ModalDialog class. I find this class very useful when creating visual webparts in SharePoint 2010 that require some sort of administrative feature or user feedback. In fact, if you look at the SharePoint 2010 architecture, you’ll notice that Microsoft have tried to avoid moving you to different pages in favor of the modal view. The SP.UI.ModalDialog class is accessible through the SharePoint JavaScript file called SP.UI.Dialog.js which is located in the layouts directory. Here is an example of how you can quickly get the SP.UI.ModalDialog class working in your code:

First we create the JavaScript function that will open up the new modal view. Notice I’m passing variables to the function for the file path, width and height. I did this so I can reuse the function for a variety of modal view calls for different purposes.

functionModalDialog(FormPath, FormWidth, FormHeight) {
varoptions = SP.UI.$create_DialogOptions();
options.url = FormPath;
options.width = FormWidth;
options.height = FormHeight;
options.allowMaximize = false;
options.dialogReturnValueCallback = Function.createDelegate(null, CloseCallback);
SP.UI.ModalDialog.showModalDialog(options);
}

We now need to create our call back method called ‘CloseCallback’ as mentioned in the above function. This method will be used to capture the results of the modal and perform some sort of confirmation action.

functionCloseCallback(result, returnValue) {
alert(‘dialogResult’ + result + ‘\nreturnValue’+ returnValue);

if(result == SP.UI.DialogResult.OK)
{
SP.UI.Notify.addNotification(‘You chose the OK button’);
document.title = returnValue;
}

if(result == SP.UI.DialogResult.cancel)
SP.UI.Notify.addNotification(‘You chose the Cancel button’);
}

You’ll see from the above method that we provide an alert with the full feedback of the modal and then utilize the SP.UI.Notify class to provide additional methods of feedback.

Now all we need to do is add an onClick of onClientClick event to an object in your code.

onclick=”ModalDialog(‘/_layouts/TestPage.aspx’,’420′,’300′)”

This is a quick introduction, but it should be enough to get you going with the SP.UI.ModalDialog class.

Read More

Customizing the SharePoint 2010 Welcome Menu (PersonalActions)

Customizing the SharePoint 2010 Welcome Menu (PersonalActions)

I support a SharePoint 2010 environment at my company and was recently asked to add an extra item to the welcome dropdown box (also know as the Personal Actions Menu) usually located at the top right of your SharePoint pages. This item was a feedback feature where users at any given location on the site could provide feedback, report a bug or add a general enhancement request.

I decided to store the data in a simple SharePoint list and implement a CustomAction Element using Visual Studio. I could have used a list view to manage the capture of the data, however I didn’t need all of the bells and whistles that came with one of those views and ultimatley created a custom application page with a form.

Creating a CustomAction is pretty easy, you just need to identify the GroupId and Location of the menu you wish to modify and then you’re good to go. You even have a simple function called a URLAction where you can direct people to a page when they click on your custom action. What isn’t so obvious is if you actually wanted your application page to popup in a modal view. Here is the code I used to get this done.

The CustomAction

Create a new blank SharePoint project in Visual Studio and then add a new ‘Empty Element’ to your project (I called mine CustomActions). Open up the CustomActions file and you should see something like this:

<?xml version=”1.0” encoding=“utf-8“?>
<Elements xmlns=“http://schemas.microsoft.com/sharepoint/“>
</Elements>

Add the following so that your file now looks like this:

<?xml version=“1.0” encoding=“utf-8“?>
<Elements xmlns=“http://schemas.microsoft.com/sharepoint/“>
<CustomAction Id=“CustomGlobalLinks”
ImageUrl=“/Assets/FeatureLogo.png”
Description=“Provide feedback or Submit an Enhancement Request”
Location=“Microsoft.SharePoint.StandardMenu”
GroupId=“PersonalActions”
Sequence=“1001”
Title=“Feedback“>
<UrlAction Url=“javascript:(function () { var o = { url:’/_layouts/CustomGlobalLinks/Feedback.aspx’, title: ‘Feedback’, allowMaximize: false, showClose: true, width: 480, height: 280 }; SP.UI.ModalDialog.showModalDialog(o); }) ();“/>
</CustomAction>
</Elements>

Let’s break this down. The ImageUrl will give you a logo to the left of your Action on the menu; this is optional. The location refers to where you want to put your action and the GroupId refers to which group on the menu you want to add it too. I’ve used the PersonActions group.

I added some custom JavaScript to the URLAction which executes SP.UI.ModalDialog.showModalDialog and displays your application page in a modal. I actually found this script on another blog, however I can’t find it any more to give credit. I’ll certainly add the credit when I find it again.

The Application Page

Add a new Application Page to your project (I called mine Feedback). Add your controls (e.g. drop downs, text boxes etc.) and then hook them up to your feedback list (already created in your SharePoint environment).

On the submission of your form, you can close your modal by calling ‘window.frameElement.commitPopup();’ Here is some quick code on pulling content from your SPList to popluate a dropdown and how to write back:

Categories Dropdown

SPList FeedbackList;
SPSite site = SPContext.Current.Site;
…………………………………………………………………

FeedbackType.Items.Add(“– SELECT –”);
SPFieldChoice cf = (SPFieldChoice)FeedbackList.Fields[“Category”];
foreach (string type in cf.Choices)
{
FeedbackType.Items.Add(type);
}
FeedbackType.DataBind();

Write back to the SPList

SPWeb web = site.RootWeb;
SPListItemCollection ItemCollection = FeedbackList.Items;
SPListItem ListItem = ItemCollection.Add();
ListItem[“Category”] = FeedbackType.SelectedItem.Text;
ListItem[“Feedback”] = FeedbackText.Text;
ListItem.Update();
successfulSubmit();

Read More