Search This Blog

Thursday, March 29, 2012

UserProfile information programmatically using SharePoint 2010

To retrieve User Profile information in SharePoint 2010 you should have User Profile Service application configured and activated. Microsoft.Office.Server.UserProfiles.UserProfileManager dll need to reference needed.

SharePoint 2010 object model code to retrieve the data:

using (SPSite site = new SPSite("Site URL"))
SPServiceContext context = SPServiceContext.GetContext(site);
UserProfileManager upm = new UserProfileManager(context);
UserProfile profile = upm.GetUserProfile("Domail\\UserName");
String WorkEmail=profile[PropertyConstants.WorkEmail].Value.ToString();
String FirstName = profile[PropertyConstants.FirstName].Value.ToString();
String LastName = profile[PropertyConstants.LastName].Value.ToString();

How to Create an InfoPath Form to Auto Populate Data in SharePoint 2010

Resources List

Create a custom list and create the fields as shown. Notice we are using “Single line of text” for the email instead of “Person or Group”.
The original “Title” column was renamed to E-Mail and requires unique values because I didn’t want someone to add themselves more than once.
The Skills column is a Lookup type that allows multiple selections tied to the Skills list.
The Title column is a new column is for the title of the resource.
Under General Settings, choose Advanced Settings. We need to change Item-Level Permissions and Attachments – all other settings are to stay at their defaults. Users should be able to read all items but only create and edit their own items; additionally we aren’t allowing attachments because they aren’t necessary for this list.
After you have the columns created for the list, it is time to customize the input form.

Customize InfoPath Form

From the Resources list, click the List tab then Customize Form.
The customize Form button will automatically launch InfoPath 2010 and will look similar to this. The red asterisks in the Fields pane indicate required fields. You can see the exact correlation on this form and our SharePoint list fields from above.

Enable SOAP Web Services

We need to enable the SOAP Web Services so we can pull information from the User Profile Service. Click the Data tab, From Web Service, From SOAP Web Service.
In the Data Connection Wizard, type the URL of your site and add /_vti_bin/UserProfileService.asmx to the end as shown and click Next
After the connection is made to the web service, scroll down to find the operation called GetUserProfileByName and click Next
Click Next on the Parameters dialog box
Click Next on the following screen
Enter a name for the data connection and click Finish.
On the right side under the Fields pane, select Show Advanced View
So far we have created our custom list, started customizing an InfoPath form and added the User Profile Service web services to the form. Now we have to create rules on what to do when the form loads.

Create Rules

Make sure you have the Data tab selected then choose Form Load. We will be using the Rules: Form Load section to create the rules and actions. Select the New button and choose Action. Our first rule is going to check the Account Name of the currently logged on user.

Account Name of Current User

Type a name for the rule under Details for then click the link under Condition that says “None – Rule runs when form is opened
In the Condition dialog box, click the arrow next to myFields and choose Select a field or group…
In the Select a Field or Group dialog box, change the Data source to GetUserProfileByName (Secondary)
Under myFields, expand queryFields until you find and select AccountName then click OK
Change the Condition is equal to” to “is blank” and click OK

Query for Data: GetUserProfileByName

In order get populate our form, we need to query the User Profile Services web service. In our Form Load data box, click Add to run an action and choose Query for data. Change the Data connection to GetUserProfileByName and click OK.
Next we need to get the data from the User Profile Services web service to populate our form with the current users’ data.

E-Mail Data

Click Add and choose Set a Field’s value
Click the expander  next to Field.
Ensure you are on the Main Fields then expand MyFields and select E-Mail and click OK
  1. Back at the Rules Details dialog box, click the Value icon  (this gets tricky :-))
  2. Insert Formula dialog box, click Insert Field or Group
  3. On the Select a Field or Group, change Fields to GetUserProfileByName (Secondary) and expand dataFields until you can select Value and click Filter Data
  4. On Filter Data, click Add
  5. In Specify Filter Conditions, change Value to Select a field or group
  6. In the Select Field or Group, and choose Name under PropertyData and click OK
  7. In the third box, change to Type Text… and type in WorkEmail and click OK on all the dialog boxes to create the rule
Let’s check to make sure we did this right. Click the Preview icon in the top left of the screen and you should see your email address populated in the E-Mail field

Monday, March 26, 2012

Working in URL in Sharepoint

While working on a project with some existing code I noticed the developer did write large portions of code to get from an URL to a SPList. He probably didn’t know some of the hidden gems in SharePoint.

Get the full URL

Sometimes you need the full URL and only have the relative one. For example when opening a new SPSite or when writing code in a NavigationProvider. For this you could use:

SPUtility.GetFullUrl(SPSite Site, string WebUrl)

For example:

string webUrl = "/sub/default.aspx";
SPUtility.GetFullUrl(SPContext.Current.Site, webUrl);
    == "http://localhost/sub/default.aspx"

There is one catch:

string webUrl = "http://localhost/sub/default.aspx";

SPUtility.GetFullUrl(SPContext.Current.Site, webUrl);
    == "http://localhosthttp://localhost/sub/default.aspx"

Check the type of URL

The former example is nice, but you would still need to write code to check if the input already contains the full URL. Nope!

For this, two gems are found in SPUrlUtility.

SPUrlUtility.IsUrlRelative(string url);

SPUrlUtility.IsUrFull(string url);

These methods do exactly what their names imply: check if the URL is relative or full. So for example:

string fullUrl = "http://localhost/sub/default.aspx";
string relUrl = "/sub/default.aspx";

SPUrlUtility.IsUrlRelative(fullUrl); == false
SPUrlUtility.IsUrlRelative(relUrl); == true
SPUrlUtility.IsUrlFull(fullUrl); == true
SPUrlUtility.IsUrlFull(relUrl); == false

Great! Now we can combine the two:

if (string.IsNullOrEmpty(webUrl) || SPUrlUtility.IsUrlRelative(webUrl))
    webUrl = SPUtility.GetFullUrl(SPContext.Current.Site, webUrl);

Now webUrl will always be the full URL.

URL concatenation

Ever did web.ServerRelativeUrl + “/something” and found out it did work nicely except it start doing something really weird on your root web? On the rootweb the relative URL is “/”, and this results in the URL “//something” which on it’s own turn gets translated to “http://something”, and that URL doesn’t exist (most of the time).

When working with file system locations, you should always use Path.Combine() instead of concatenating path’s yourself. But the is no Uri.Combine().

You could write an extension method. But the SharePoint team made it more easy.

SPUrlUtility.CombineUrl(string baseUrlPath, string additionalNodes)

This method does the same thing like Path.Combine(). For example:

string root = "/";
string path = "/sub"
string doc = "/sub/default.aspx";

SPUrlUtility.CombineUrl(root, path); == "/sub"
SPUrlUtility.CombineUrl(root, doc); == "/sub/default.aspx"
SPUrlUtility.CombineUrl(path, doc); == "/sub/sub/default.aspx"

That’s the final (hidden) gem for today.


Step 1:

Go to Infopathà choose blank form

Step 2: Select container selection option and set Rules for Date

Step 3:

Right click the Date TextBox control and Go to Properties:

And set value: substring-before(now(), "T")

Step 4:

Right click the Time TextBox control and Go to properties:

And set value: substring-after(now(), "T")

Step 5:

In Data Tab à Click the submit option

Select the document library optionà Choose a data connection for submit à Add

Step 6:
Set File name: clickand select Insert Function and choose concat and select the fields like: concat("save", Date, "-", Time)

After the fields are set, click Next button and Finish.

Step 7:

Select File TabàPublishàSharepoint server and publish Your site.

Friday, March 23, 2012

How to Create an ASMX Web Service on SharePoint 2010, Using Visual Studio 2010

in SharePoint 2007, asmx web services were quite prevalent, thanks to the
WSPBuilder tool, and it’s templates. They are useful for executing
actions between multiple web applications and can be used by client
applications, as well. Furthermore, InfoPath forms, deployed to
SharePoint, could also use these asmx web services.

Visual Studio 2010 did not come with a template for SharePoint web
services. So, today I will be writing about how we can create asmx web
services for SharePoint 2010. All you will need is SharePoint 2010.

start a new Empty SharePoint 2010 project. I will call this SPASMXService.

sure to deploy it as a farm solution.

you need to close this project by right clicking on the project and then
selecting ‘unload project’.

right click on the project again, and select, ‘Edit SPASMXService’.

Under <SandboxedSolution>False</SandboxedSolution> type in:


This will be the result:

Then, save and close out of this xml
file. This will allow Visual Studio to insert the solution information
where necessary. Therefore, this is a crucial step! Finally, reload
the project file.

Next, we will be creating the web
service. Right click on the project, and select “Add” and then select
“New Class…” This will be the code behind. Let’s just call this SPASMXService.cs.

Now, open SPASMXService.cs, and make
the following changes:

This is a good start for a web
service with a web method. Now, of course we have a few errors because we
still have not brought in the necessary libraries. Right click on
‘references’ in the Solution Explorer, and select, ‘Add Reference’.
Select System.Web.Services from the .NET tab. Then, in SPASMXService.cs,
add, ‘using System.Web.Services’. This should look like this:

Finally, we have to create the
service page. I like to keep it in the _layouts folder, but you can keep
it elsewhere using similar steps. Right click on the project item in the
solution explorer, and select add -> SharePoint “Layouts” Mapped Folder.

You can also select SharePoint
Mapped Folder, and then select ISAPI. This would cause the page to go
into _vti_bin instead.

For now, I’m going to stick to

The SPASMXService folder was
automatically made. Nice.

Inside the SPASMXService, under
Layouts, we will add a new file of type xml. We Shall call it SPASMXService.asmx.

The contents of SPASMXService.asmx
will be a single line:

<%@ WebService Language="C#" Debug="true" Class="[Class path], $SharePoint.Project.AssemblyFullName$"


Where [class path] is the full
namespace name of the SPASMXService class in SPASMXService.cs. In my
case, the line will be:


Finally, save everything, and then
deploy the solution.

If everything went right, you should
see this using Internet Explorer:

If you used ISAPI instead of
Layouts, _layouts in that screenshot should be _vti_bin, instead. If you
opened this from a front end server with web service, then you can further test
this web service by clicking on that link.

Lastly, a bit of trouble shooting;
you can check on the web service page by going to:

C:\Program Files\Common
Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS

If you used ISAPI instead of
LAYOUTS, then instead go to:

C:\Program Files\Common
Files\Microsoft Shared\Web Server Extensions\14\ISAPI

If the web service does not load on
Internet explorer, then you should open the asmx page from one of these two
locations. If you open the asmx page from one of these two locations, and
you still find “$SharePoint.Project.AssemblyFullName$”,
then you need to go back to the top of this article and follow the steps
regarding unloading and reloading the project.