Wednesday, August 27, 2008

Add my Links using Custom Code

1. Create aspx page and add ASP button to this page. (aspx)
2. Copy the below mention code and paste to Button click events (aspx.cs)
3. Copy the aspx and aspx.cs file and paste to the _layout folder. (IIS --> 80 --> _layout--> Open)
4. Run the custom page through IIS
try
{
#region Basic Declaration
string strTitle = string.Empty;
string sLinkUrl = string.Empty;
string strGroup = string.Empty;
Microsoft.Office.Server.UserProfiles.UserProfile oUserProfile = null;
UserProfileManager upm = null;
SPSite site = null;
ServerContext server = null;
#endregion

#region Open SSP Site and Security Settings
site = new SPSite("SSP Website URL");
SPWeb myWeb = site.OpenWeb();
// Allow to Custom Updation
myWeb.AllowUnsafeUpdates = true;
server = ServerContext.GetContext(site);
upm = new UserProfileManager(server, false);
#endregion
// Assign the User Name
string sAccount = "Domain Name" + "\\" + "Account Name".ToString();
// Assign the Title
strTitle = "Title Name".ToString();
strWorkspace = "Title".ToString();
// Assign the Site URL
sLinkUrl = "Web Site URL";
strGroup = "General";
if (!upm.UserExists(sAccount))
{
// User Profile Not Exist means we need to create the user profile upm.CreateUserProfile(sAccount);
// Get the userprofile
oUserProfile = upm.GetUserProfile(sAccount);
QuickLinkManager qlm = oUserProfile.QuickLinks;
// Add my links to Sharepoint Personal Sites
QuickLink q1 = qlm.Create(strTitle, sLinkUrl, QuickLinkGroupType.General, strGroup, Privacy.Private); oUserProfile.Commit();
Response.Write("Account Created & My Links Added Successfully");
}
else
{
// Get the userprofile
oUserProfile = upm.GetUserProfile(sAccount);
QuickLinkManager qlm = oUserProfile.QuickLinks;
// Add my links to Sharepoint Personal Sites
QuickLink q1 = qlm.Create(strTitle, sLinkUrl, QuickLinkGroupType.General, strGroup, Privacy.Private);
oUserProfile.Commit();
Response.Write("My Links Added Successfully");
}
}
catch (Exception ex)
{ throw ex;
}

No comments: