Posts Tagged ‘SharePoint’
Few days ago I add to copy Alerts and QuickLinks from a UserProfile to another in SPS 2003. I managed to create the functions, it seems easy at first, but the one thing I forgot is that I was on .Net Framework 1.1 withVisual Studio 2003!! argh! intellisense sucks compare to VS2008 or 2005
Copy QuickLinks:
/// </summary>
/// copy QuickLinks to another userprofile
/// copy if not exist, update otherwise
/// </summary>
/// <param name=”userProfile”>UserProfile that will contain the links</param>
/// <param name=”links”>Links to copy</param>
public static void CopyQuickLinks(UserProfile userProfile,QuickLinkManager links)
{
foreach(QuickLink link in links)
{
QuickLink newLink = null;
newLink= userProfile.QuickLinks.Add(link.Title,link.URL,link.Group,link.Public);
newLink.ContentClass= link.ContentClass;
newLink.Commit();
}
}
Copy Alerts:
///<summary>
/// copy a list of a alert to another userprofile
/// copy if not exist, update otherwise
/// </summary>
/// <param name=”userProfile”>UserProfile that will contain the alerts</param>
/// <param name=”alerts”>alerts to create for user</param>
public static void CopyAlerts(UserProfile
userProfile,ArrayList alerts)
{
foreach(Microsoft.SharePoint.Portal.Alerts.Alert alert in
alerts)
{
Microsoft.SharePoint.Portal.Alerts.Alert newAlert = null;
if(userProfile.Alerts[alert.Name] == null)
{
newAlert=
userProfile.Alerts.CreateAlert();
newAlert.Type = alert.Type;
foreach(Microsoft.SharePoint.Portal.Alerts.DeliveryChannelSettings channelSettings in alert.DeliveryChannels)
{
newAlert.DeliveryChannels.Add(channelSettings);
}
}
else
{
newAlert =
userProfile.Alerts[alert.Name];
}
newAlert.Name = alert.Name;
newAlert.ObjectDisplayName =
alert.ObjectDisplayName;
newAlert.ObjectID = alert.ObjectID;
newAlert.ObjectUrl = alert.ObjectUrl;
newAlert.Query = alert.Query;
newAlert.ConditionText =
alert.ConditionText;
newAlert.QueryLocale =
alert.QueryLocale;
newAlert.Commit();
if(alert.Active)
newAlert.Activate();
else
newAlert.Deactivate();
}
}
Since System.Collections is pretty limited in .net 1.1 I used an ArrayList of Alerts, which contains the alerts of the user to copy from
/// <summary>
/// Returns a StringCollection object from a SPField object of type SPFieldChoice from the web.Fields
/// </summary>
/// <param name=”fieldName”>Field name needed to retrieve the list of choice of the SPFieldChoice object</param>
/// <returns></returns>
public static StringCollection GetAllFieldChoices(string fieldName)
{
StringCollection fieldChoices = null;
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb origWeb = SPContext.Current.Web;
while (!origWeb.IsRootWeb)
origWeb = origWeb.ParentWeb;
try
{
using (SPSite site = new SPSite(origWeb.Url))
{
using (SPWeb web = site.OpenWeb())
{
SPField field = web.Fields[fieldName] as SPField;
if (field != null)
{
if (field.Type == SPFieldType.Choice)
fieldChoices = ((SPFieldChoice)field).Choices;
}
}
}
}
catch (Exception ex)
{
throw;
}
});
return fieldChoices;
}
/// <summary>
/// Update an SPFieldChoice Field of the SPWeb
/// </summary>
/// <param
name=”fieldName”>Name of the
field</param>
/// <param
name=”choices”>StringCollection
that represents the choices for the field</param>
public static void
UpdateSPFieldChoice(string fieldName,
System.Collections.Specialized.StringCollection
choices)
{
SPSecurity.RunWithElevatedPrivileges(delegate()
{
SPWeb
origWeb = SPContext.Current.Web;
while
(!origWeb.IsRootWeb)
origWeb = origWeb.ParentWeb;
try
{
using
(SPSite site = new
SPSite(origWeb.Url))
{
using
(SPWeb web = site.OpenWeb())
{
SPField
field = web.Fields[fieldName] as SPField;
if
(field != null)
{
if
(field.Type == SPFieldType.Choice)
{
int
numberOfChoices = choices.Count;
string[]
values = new String[numberOfChoices];
choices.CopyTo(values, 0);
web.AllowUnsafeUpdates = true;
SPFieldChoice
spFied = field as SPFieldChoice;
if
(spFied != null)
{
spFied.Choices.Clear();
spFied.Choices.AddRange(values);
spFied.Update();
}
}
}
}
}
}
catch (Exception ex)
{
throw;
}
});
}