The following misc code snippets are written in C# for the SharePoint API version 3.0.
using Microsoft.SharePoint;
private function bool siteUsers()
{
SPSite siteCollection = new SPSite("http://localhost/");
SPUserCollection SPUsers = siteCollection.AllWebs["/"].Users; // root site
foreach (SPUser SPUser in SPUsers)
{
Console.Write(SPUser.Name + "\r\n");
}
return true;
}
private function doFoobar()
{
Delete/Update SharePoint list items...
SPSite siteCollection = new SPSite("http://localhost/");
System.Net.NetworkCredential auth = new System.Net.NetworkCredential("userName", "password", "domain");
siteCollection.credentials = auth;
SPList list = siteCollection.AllWebs["sub_web_x"].Lists["List_Name"];
/**********************************************
Delete Existing SP list items
**********************************************/
SPQuery query = new SPQuery();
query.Query = ""; // Send no conditions in query. (btw, this is a CAML query type)
SPListItemCollection listItems = list.GetItems(query);
int listItemsCount = listItems.Count;
for (int i = 0; i < listItemsCount; i++)
{
listItems.Delete(0); // becuase the index slides as items are deleted, we simply delete the 0 index item repeatedly.
}
/**********************************************
Load up records from a db table
**********************************************/
SqlConnection conn = new SqlConnection();
SqlCommand cmd = new SqlCommand();
conn.ConnectionString = "Data Source=db_Server;User ID=foo;Password=bar;Database=personel_db";
conn.Open();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM vContacts";
SqlDataReader dataReader = cmd.ExecuteReader();
// Load up records into SharePoint lists.
int x = 0;
while (dataReader.Read())
{
SPListItem newItem = listItems.Add();
newItem["Last Name"] = dataReader["LastName"];
newItem["First Name"] = dataReader["FirstName"];
newItem["Middle Initial"] = dataReader["Minitial"];
newItem["Nickname"] = dataReader["NickName"];
newItem["Company"] = dataReader["Company"];
newItem["Department"] = dataReader["Department"];
newItem["Customer Phone"] = dataReader["CustomerPhone"];
newItem["TEAS Office Phone"] = dataReader["TEASPhone"];
newItem["E-mail Address"] = dataReader["EmpEmail"];
newItem["Username"] = dataReader["UserName"];
newItem.Update();
}
conn.Close();
return true;
}