MOSS SHAREPOINT TIPS AND TRICKS
Test whether an SPUser object was a member of an arbitrary SPGroup
private bool bIsInGroup(SPUser user, string strname)
{
try
{
foreach (SPGroup group in user.Groups)
{ if (group.Name == strname) return true;
}
}
catch (Exception exception)
{
WriteLogEvent(string.Format("An Error Occured | Exception Message:{0} StackTrace: {1}", exception.Message,
excepticatch (Exception ex)
{
WriteLogEvent(string.Format("Err Occured|Exception:{0}tackTrace:{1}",ex.Message,ex.StackTrace));
} = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPList contactsList = myWeb.Lists["Contacts"];
foreach (SPListItem contact in contactsList)
{
string contactLastName = contact["Last Name"].ToString(); }
}
}
change all items in the list to build the email address based on the first and
last name
using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPList contactsList = myWeb.Lists["Contacts"];
foreach(SPListItem existingItem in contactsList.Items)
{
existingItem["E-mail Address"] = existingItem["First Name"] + "." + existingItem["Last Name"] + "@testing.com";
newItem.Update();
}
}
}
}
File properties are available through its Item property
using(SPSite mySite = new SPSite("http://server/sites/site"))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPFile file = myWeb.GetFile("http://server/sites/site/library/folder/file");
string filePropertyValue = file.Item["My Custom String Property"].ToString();
}
To get to a document in a document library
using(SPSite mySite = new SPSite("http://server/sites/site")) uot;))
{
using(SPWeb myWeb = mySite.OpenWeb())
{
SPFile file = myWeb.GetFile("http://server/sites/site/library/folder/file");
}
}
Using the SharePoint PeoplePicker control in a web part
public class PeoplePickerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{ PeopleEditor pe;
TextBox t;
Button b;
protected override void CreateChildControls()
{
base.CreateChildControls();
pe = new PeopleEditor();
this.Controls.Add(pe);
b = new Button();
b.Text = "Click me to see the users";
this.Controls.Add(b);
b.Click += new EventHandler(b_Click);
t = new TextBox();
t.TextMode = TextBoxMode.MultiLine;
this.Controls.Add(t);
}
void b_Click(object sender, EventArgs e)
{
foreach(string ent in pe.CommaSeparatedAccounts.Split(','))
{
t.Text += ent + Environment.NewLine;
}
}
Using the SharePoint date control in a web part
public class DatePickerWebPart : System.Web.UI.WebControls.WebParts.WebPart
{ DateTimeControl dtc;
TextBox t;
Button b;
protected override void CreateChildControls()
{ base.CreateChildControls(); dtc = new DateTimeControl();
dtc.ID = "dtc" + this.UniqueID; this.Controls.Add(dtc);
b = new Button(); b.Text = "Click me to see the date";
this.Controls.Add(b);
b.Click += new EventHandler(b_Click);
t = new TextBox();
this.Controls.Add(t); } void b_Click(object sender, EventArgs e)
{
t.Text = dtc.SelectedDate.ToLongDateString();
}
}
}
.
.