Archive

Posts Tagged ‘microsoft sharepoint’

Creating Custom Profile Property in SharePoint Server 2010

March 31, 2013 Leave a comment

if you have requirements to create Custom Profile Property in SharePoint 2010

below function do That

private
void CreateCustomProperty(string profilePropertyText,SPSite siteColl)

{


SPSecurity.RunWithElevatedPrivileges(delegate()

{

siteColl.AllowUnsafeUpdates = true;

siteColl.OpenWeb().AllowUnsafeUpdates = true;


//SPContext.Current.Web.AllowUnsafeUpdates = true;


SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);


UserProfileConfigManager profileConfigManager = new
UserProfileConfigManager(serviceContext);


CorePropertyManager coreProperties = profileConfigManager.ProfilePropertyManager.GetCoreProperties();


try

{


List<string> MyCustomProperties = new
List<string>();

MyCustomProperties.Add(profilePropertyText);


ProfilePropertyManager propertyManager = profileConfigManager.ProfilePropertyManager;


foreach (string profileProperty in MyCustomProperties)

{


if (coreProperties.GetPropertyByName(profileProperty) == null)

{


CoreProperty propertyInstance = coreProperties.Create(false);

propertyInstance.Name = profileProperty.Replace(” “, string.Empty);

propertyInstance.Type = PropertyDataType.String;

propertyInstance.Length = 2000;


//propertyInstance.Separator = MultiValueSeparator.Semicolon;

propertyInstance.DisplayName = profileProperty;

propertyInstance.Description = profileProperty;

propertyInstance.IsAlias = false;

propertyInstance.IsSearchable = true;

propertyInstance.Commit();


ProfileTypePropertyManager profileTypePropertyMgr = propertyManager.GetProfileTypeProperties(ProfileType.User);


ProfileTypeProperty profileTypeProperty = profileTypePropertyMgr.Create(propertyInstance);

profileTypeProperty.IsVisibleOnViewer = true;

profileTypeProperty.IsVisibleOnEditor = true;

profileTypePropertyMgr.Add(profileTypeProperty);


ProfileSubtypeManager psm = ProfileSubtypeManager.Get(serviceContext);


ProfileSubtype ps = psm.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));


ProfileSubtypePropertyManager pspm = ps.Properties;


ProfileSubtypeProperty psp = pspm.Create(profileTypeProperty);

psp.IsUserEditable = true;


//psp.Commit();

pspm.Add(psp);

}

}

siteColl.AllowUnsafeUpdates = false;


//SPContext.Current.Web.AllowUnsafeUpdates = false;

siteColl.OpenWeb().AllowUnsafeUpdates = true;

}


catch (DuplicateEntryException exception)

{

siteColl.AllowUnsafeUpdates = false;


SPContext.Current.Web.AllowUnsafeUpdates = false;

logMessage(exception.Message, EventLogEntryType.Error);

}

});

}

to check if specific Profile Property exist or not

private
bool CheckCustomPropertyExistance(string profileProperty, SPSite siteColl)

{


bool exist = false;


SPSecurity.RunWithElevatedPrivileges(delegate()

{


SPServiceContext serviceContext = SPServiceContext.GetContext(siteColl);


UserProfileConfigManager profileConfigManager = new
UserProfileConfigManager(serviceContext);


CorePropertyManager coreProperties = profileConfigManager.ProfilePropertyManager.GetCoreProperties();


try

{


if (coreProperties.GetPropertyByName(profileProperty) != null)

{

exist = true;

}

}


catch (DuplicateEntryException exception)

{

logMessage(exception.Message, EventLogEntryType.Error);

}

});


return exist;

}

Creating a custom WCF service in SharePoint Server 2010

October 11, 2012 Leave a comment

Developing a WCF in SharePoint 2007 was not easy Job. and for now in SharePoint Server 2010 ASP.NET web services are obsolete . in SharePoint 2010, the recommended approach is developing WCF. Creating Custom WCF and hosting it in SharePoint 2010 isn’t difficult task where we can achieve that through multiple approaches like using an open source Visual Studio 2010 extension know as Community Kit for SharePoint: Development Tools Edition which makes developing Custom WCF more easily or Creating Empty SharePoint Project , then Map to ISAP folder then add WCF files ( Interface , Classes, Configuration Files) to it.

Here i going to use Community Kit for SharePoint . The Community Kit for SharePoint: Development Tools Edition extends the Visual Studio 2010 SharePoint project system with advanced templates and tools.

Create WCF Service

1- create a new empty SharePoint Project.

2- Select Deploy as farm solution.

3- Add new Item, Right Click on Project Name , then Select WCFService (CKSDEV).

Services Type

There are 3 different type of service

1- SOAP Service.

2- REST Service

3- ADO.NET Data Service

if you want to change the service type,please modify the factory in .SVC

SOAP Service Factory : MultipleBaseAddressBasicHttpBindingServiceHostFactory

REST Service Factory: MultipleBaseAddressWebServiceHostFactory

ADO.NET Service Factory : MultipleBaseAddressDataServiceHostFactory

Browsing Custom WCF Service

After Service deploy, you need to use the service in another projects. First try to access the service in browser. But remember you need to access the MEX endpoint either you will not get the service accessible in browser. To access the MEX endpoint, you should add “/MEX” at the end of the service name as shown below:

How to Display Presence Indicator in Custom Webparts

March 21, 2012 4 comments

SharePoint Server 2010 gives the ability to identify the status of co-works or site members if they online , busy, or away by integrating with Presence Indicator Information concept.  smart tag appears next to the user name to display the user status .

The key piece of information to make the presence work is the user’s SIP Address which is basically their IM address (This is not always the same as email address). When a users is either added to a site in SharePoint or has their profile imported, the SIP Address will be drawn from Active Directory which is where OCS stores it and placed into the SIP address field in either the user’s profile which will in turn synchronise down to the site’s local ‘User Information’ page.

The pawn is basically an IMG element which has “IMNRC(‘[user’s SIP Address]’)” for the onload function. This will user client-side script that is part of Office to load the presence pawn on the page.

The hyperlink on the user’s name is just a simple A element which redirects to “/_layouts/userdisp.aspx?ID=[User’s local user information list item ID]”. This userdisp.aspx page will then redirect to the user’s main profile page if they have a profile, otherwise it will display the basic information that the user information list item stores.

How to get the SIP Address?

Hopefully you’ll have spotted by now that the presence pawn relies on the user’s SIP Address. To get the SIP address you need to get the user’s User Information list item from the local site’s (SPWeb to be precise) SiteUserInfoList which is basically a hiddenSPList that is in every web.

There is a handy property to get you to this list called ‘SPWeb.SiteUserInfoList’. This will give you an SPList object which represents the User Information list. From here you just need to find the item that represents the user you wish to display. The best way to do this is via their ID (the ID of the list item) by calling SPWeb.SiteUserInfoList.GetItemById([int User’s ID]), however you can also use a variety of other methods which use SPQuery or match a specific field to a value.

In most scenarios, you may be getting the user information from a SPFieldUserValueCollection which is basically the field type that is used for ‘Person’ fields. If this is the case you can use SPFieldUserValue.LookupId to get the ID of the user’s User Information list item.

Putting it all together

This code sample is a method that accepts an SPFieldUserValueCollection and SPSite as inputs and returns back the HTML for displaying each entry in the SPFieldUserValueCollection with the presence pawn and link to their profile. This will be presented exactly as SharePoint presents it by default. This could be extended to use ALT tags in the IMG and A elements.

I then simply add the HTML to an HtmlWriter or in my case a TableCell.Text property to display it on screen.

I’ve take a few extra steps here by adding ID and alt tags and trying to make the code readable, but I’m sure you get the idea:

public string GetPresenceHTMLFromUVC(SPSite thisSite, SPFieldUserValueCollection uvc)
{
StringBuilder html = new StringBuilder(“”);
using (SPWeb thisWeb = thisSite.RootWeb)
{
foreach (SPFieldUserValue uv in uvc)
{
string sipAddress = “”;
//Get the sip address if it exists
SPListItem user = thisWeb.SiteUserInfoList.GetItemById(uv.LookupId);
if (user[“SipAddress”] != null)
{
sipAddress = user[“SipAddress”].ToString();
}
//construct the html for this user and add to overall html
html.Append(String.Concat(
“<div id\”PresenceLink”
, sipAddress
, “\”>”
, “<img border=\”0\” height=\”12\” src=\”/_layouts/images/imnhdr.gif\” onload=\”IMNRC(‘”
, sipAddress
, “‘)\” ShowOfflinePawn=\”1\” style=\”padding-right: 3px;\” id=\”PresencePawn”
, sipAddress
, “\” alt=\”presence pawn for ”
, sipAddress
, “\”/>”
, “<a href=\””
, thisWeb.Url
, “/_layouts/userdisp.aspx?ID=”
, uv.LookupId
, “\” id=\”ProfileLink”
, sipAddress
, “\”>”
, uv.User.Name
, “</a></div>”
));
}
}
return html.ToString();
}

 

references:-

http://blogs.msdn.com/b/uksharepoint/archive/2010/05/07/office-communicator-integration-presence-in-a-custom-webpart-for-sharepoint-2010.aspx

http://blog.sharepointsite.co.uk/2012/02/status-indicator-not-working.html

 

 

SharePoint 2010 : Retrieve User Profile Properties Programmatically

March 17, 2012 Leave a comment

To display the user profile properties that exist on the server Also add references to the following in your Microsoft Visual Studio project:

  • Microsoft.Office.Server
  • Microsoft.Office.Server.UserProfiles
  • Microsoft.SharePoint
  • System.Webhere, in below example, we need to check if  propertyName exits in user profile properties store or not.

    private bool CheckIFPropertyExist(string propertyName, string siteName)
    {
    using (SPSite currentSite = new SPSite(siteName))
    {
    SPServiceContext context = SPServiceContext.GetContext(currentSite);
    ProfileSubtypeManager profileMgr = ProfileSubtypeManager.Get(context);
    ProfileSubtype profileSubType = profileMgr.GetProfileSubtype(ProfileSubtypeManager.GetDefaultProfileName(ProfileType.User));
    ProfileSubtypePropertyManager profilePropertyMgr = profileSubType.Properties;

    foreach (ProfileSubtypeProperty profileSubtypeProperty in profilePropertyMgr.PropertiesWithSection)
    {
    if (profileSubtypeProperty.Name != propertyName)
    {
    return false;
    }
    }
    return true;

    }

    }