Archive

Archive for March, 2013

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;

}

Using List Picker Control in Custom WebPart Properties

March 31, 2013 2 comments

if you are developig custom webpart and one of the requirements is getting list title as webpart property. so, you can do that by just adding custom Property as text and the user write list title and you in code behind will handle and validate list title and check if this list already exist or not. Also if you want to query , add or update list in subsite , you will need to add relative path to the list. to override all of these , you can add Out of the Box List Picker, just you will develop custom toolpane for the list and add TextBox and Button to handle user Click.

Let us do an example ,

1-firstly, we need to create custom toolpane Class as the following, here, we need two controls TextBox to get Full path of selected List and Button to handle User Click.

namespace SharePoint14.Webparts.QuickLinkWebPart

{


public
class
ListPicker: EditorPart

{


TextBox txt;


Button btn;

2-then add Custom ToolPane ID and Title in Construct ,also define Control Init function.

public ListPicker(string webPartID, string webpartname)

{


this.ID = “listpicker” + webPartID;


this.Title = “Custom WebPart Settings”;


//this = webpartname;


this.Init += new
EventHandler(ImagePickerToolPart_Init);

}

3- define Init Function, in this function will add define Script Link Control , and in Name Property add “AssetPickers.JS” Load OOTB JavaScript File which is responsible for Opening nice picker dialog where you can choose the List

private
void ImagePickerToolPart_Init(object sender, EventArgs e)

{

txt = new
TextBox();

btn = new
Button();

sl = new
ScriptLink();

lit = new
Literal();

sl.Name = “AssetPickers.js”;

sl.Localizable = true;

sl.Language = “javascript”;

btn.Text = “…”;

SyncChanges();

}

4- Define CreateChildControls()

protected
override
void CreateChildControls()

{


base.CreateChildControls();

Controls.Add(sl);


this.Controls.Add(new
Label() { Text = “Select a List” });


this.Controls.Add(new
HtmlGenericControl(“br”));

Controls.Add(txt);

Controls.Add(btn);


string strRelativeWebURL = SPEncode.HtmlEncode(SPControl.GetContextWeb(Context).ServerRelativeUrl);


if (strRelativeWebURL.Trim() ==“/”)

{

strRelativeWebURL =“”; //blank browses the root spweb

}


string strAssetPickerConfig = string.Format(@”

<script type=’text/javascript’>

with(new AssetPickerConfig(‘testAssetPickerObj’))

{{

DefaultAssetImageLocation=”;

CurrentWebBaseUrl='{0}’;

OverrideDialogFeatures=”;

OverrideDialogTitle=”;

OverrideDialogDesc=”;

OverrideDialogImageUrl=”;

AssetUrlClientID='{1}’;

AssetTextClientID=”;

UseImageAssetPicker=true; //make this false to show Documents instead

DefaultToLastUsedLocation=true;

DisplayLookInSection=true;

ReturnCallback = null;}}

</script>”,

strRelativeWebURL,

txt.ClientID);

lit.Text = strAssetPickerConfig;

Controls.Add(lit);

btn.OnClientClick = “APD_LaunchAssetPickerUseConfigCurrentUrl(‘testAssetPickerObj’); return false;”;

}

i hope this help you