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(); 
} 
} 
} 

.

.
Copy Discover the Current User’s Authorization Level
To find out if the current user is a member of a Site Group, use the following method where you 
would pass in the "role" as the name of the Site Group: 

        private bool IsCurrentUserInRole(string role)
        {
            bool inRole= false; 

            SPWeb rootWeb = SPControl.GetContextSite(Context).RootWeb;
            SPRole  spRole = rootWeb.Roles[role];
            SPUser currentUser = rootWeb.CurrentUser;
   
            foreach(SPUser roleUser in spRole.Users)
            {
                if(roleUser.ID.Equals(currentUser.ID))
                {
                    inRole= true;
                    break;
                }
            } 

            return inRole;
        }
Copy Query Database to Find Large Documents
Problem Statement 

You are the SharePoint Administrator for your company. Lately, you have noticed that many people
in the organization have been uploading large documents to their sites. You have also noticed that
 at certain times in the day, your servers are bogged down and working at full capacity. 
You suspect the reason to be that numerous people are uploading and downloading large 
documents from their sites at those times. You need to identify these bottlenecks 

Solution 


If you have a large SharePoint installation at your company, you probably don't want to manually 
navigate to each site to look for large documents. Even if you did go through the excruciating 
process this time around, you will not be able to repeat it easily the next time you have the need. 


An efficient and quick way to look for all large documents in your installation is to query the database directly (Disclaimer: If you have the manpower, it is usually better and safer to develop an application which uses the object model to gather this information instead). The content database contains all the documents that have been uploaded to any site in your SharePoint installation. If you have created additional content databases, then you would need to query all of them.
Let's say if you wanted to search for the top 100 large documents in your content database. You can use the following query to accomplish this:
SELECT TOP 100 DirName + '/' + LeafName AS [name], DATALENGTH(Content) AS contentlen FROM Docs ORDER BY contentlen DESC This query will return the names of the documents and their location which need your attention. Update for WSS v3: Following is the updated query for the WSS v3 version: SELECT TOP 100 DirName + '/' + LeafName AS [name], [Size] AS contentlen FROM AllDocs ORDER BY contentlen DESC