String FirstName = profile[PropertyConstants.FirstName].Value.ToString();
Search This Blog
Thursday, March 29, 2012
UserProfile information programmatically using SharePoint 2010
String FirstName = profile[PropertyConstants.FirstName].Value.ToString();
How to Create an InfoPath Form to Auto Populate Data in SharePoint 2010
Resources List
Customize InfoPath Form
Enable SOAP Web Services
Create Rules
Account Name of Current User
Query for Data: GetUserProfileByName
E-Mail Data
- Back at the Rules Details dialog box, click the Value icon (this gets tricky :-))
- Insert Formula dialog box, click Insert Field or Group
- On the Select a Field or Group, change Fields to GetUserProfileByName (Secondary) and expand dataFields until you can select Value and click Filter Data
- On Filter Data, click Add
- In Specify Filter Conditions, change Value to Select a field or group
- In the Select Field or Group, and choose Name under PropertyData and click OK
- In the third box, change to Type Text… and type in WorkEmail and click OK on all the dialog boxes to create the rule
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.
InfoPath-Submit
Set File name: clickand select Insert Function and choose concat and select the fields like: concat("save", Date, "-", Time)
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.
Unfortunately,
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.
First,
start a new Empty SharePoint 2010 project. I will call this SPASMXService.
Make
sure to deploy it as a farm solution.
First,
you need to close this project by right clicking on the project and then
selecting ‘unload project’.
Then,
right click on the project again, and select, ‘Edit SPASMXService’.
Under <SandboxedSolution>False</SandboxedSolution> type in:
<TokenReplacementFileExtensions>asmx</TokenReplacementFileExtensions>
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
_layouts:
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:
From:
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.