Monday, January 19, 2009

How to insert sharepoint user to a sharepoint user Group in c#

using Microsoft.SharePoint;


public void AddUserToSPGroup(String _UserName, string _GroupName)
{
try
{
SPSite site = new SPSite(ConfigurationManager.AppSettings["SITE_URL"].ToString());
SPWeb web = site.AllWebs[ConfigurationManager.AppSettings["WEB_SITE"].ToString()];

//SPUser spUser = web.AllUsers[domain + "\\" + userName];
SPUser spUser = web.AllUsers["ad:" + _UserName];

//Open group
SPGroup spGroup = web.SiteGroups[_GroupName];

//Add and update group with new user
web.AllowUnsafeUpdates = true;
spGroup.AddUser(spUser.LoginName, spUser.Email, spUser.Name, "Added by UserControl");
spGroup.Update();
}
catch (Exception ex)
{
lblUserList.Text = ex.Message.ToString();
}
}

Sunday, January 18, 2009

How to Send SMS - SMPP in c#

How to Read the Url of a Web page dynamically in c#

You can get the the url of the currently viewing web page using


HttpContext.Current.Request.ServerVariables["URL"].ToString() ;

How To Generate Random Numbers And Strings in c#

This function can be effectively used for Gnerating Random strings of any length


public string RandomStrings()
{

StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch;
for (int i = 0; i < 10; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
builder.Append(ch);
}

return builder.ToString();

}


You can use The following code to generate random numbers

private string RandomString(int size, bool lowerCase)
{
StringBuilder builder = new StringBuilder();
Random random = new Random();
char ch ;
for(int i=0; i==size; i++)
{
ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65))) ;
builder.Append(ch);
}
if(lowerCase)
return builder.ToString().ToLower();
return builder.ToString();
}

How to change Active directory user properties in c#

// Create a DirectorySearcher object using the user name as the LDAP search filter. If using a directory other than Exchange, use sAMAccountName instead of mailNickname.
DirectorySearcher searcher = new DirectorySearcher("(cn=" + _UserName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();

// Make sure the user was found.
Response.Write(_UserName);
// Create a DirectoryEntry object to retrieve the collection of attributes (properties) for the user.
DirectoryEntry user = result.GetDirectoryEntry();

domain = ConfigurationManager.AppSettings["DOMAIN"].ToString();

user.Username = domain + "\\" + ConfigurationManager.AppSettings["USERNAME"].ToString();
user.Password = ConfigurationManager.AppSettings["PASSWORD"].ToString();
user.AuthenticationType = AuthenticationTypes.Secure;



if (details[1] != "")
{
user.Properties["TelephoneNumber"].Value = details[1];
}

if (details[2] != "")
{
user.Properties["streetAddress"].Value = details[2];
}

if (details[3] != "")
{
user.Properties["Description"].Value = details[3];
}

if (details[4] != "")
{
user.Properties["mail"].Value = details[4];
}
if (details[8] != "")
{
user.Properties["sn"].Value = details[8];//last name
}
if (details[9] != "")
{
user.Properties["displayName"].Value = details[9];//display name
}
if (details[10] != "")
{
user.Properties["givenName"].Value = details[0];//1st name
}
if (details[11] != "")
{
user.Properties["physicalDeliveryOfficeName"].Value = details[11];//office phone
}
if (details[12] != "" && details[13] != "")
{
user.Properties["co"].Value = details[12];//country name
user.Properties["c"].Value = details[13];//country code
}

user.CommitChanges();

if (details[5] != "" & details[6] != "")
{
if (details[5].Equals(details[6]))
{
user.Invoke("SetPassword", new object[] { details[5] });
user.CommitChanges();

}
else
{
lblUserList.ForeColor = Color.Red;
lblUserList.Text = "Password Mismatch";
return false;
}
}

// Clean up.
searcher.Dispose();
result = null;
user.Close();

How to insert Active directory users to an Active directory Group in c#

public void AddUserToADGroup(DirectoryEntry de,string _Group,string _UserName)
{
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(objectClass=user)";
ds.Sort.Direction = System.DirectoryServices.SortDirection.Ascending;
ds.SearchScope = System.DirectoryServices.SearchScope.Subtree;
ds.PageSize = 4000;

string name = "";
try
{
SortedList objSortedList = new SortedList();
foreach (SearchResult result in ds.FindAll())
{
DirectoryEntry deTemp = result.GetDirectoryEntry();
name = deTemp.Name;
try
{
name = deTemp.Properties["cn"].Value.ToString();
if (name.Equals(_UserName))
{
DirectoryEntry objGrp = de.Children.Find("CN=" + _Group);

//adding new user to group
if (objGrp.Name != "")
{
objGrp.Invoke("Add", new object[] { deTemp.Path.ToString() });
objGrp.CommitChanges();
deTemp.CommitChanges();
de.CommitChanges();
}
}
}
catch (Exception ex)
{
ex.Message.ToString();
}
}
//FillGroup(ddlEmpName);
}
catch (Exception ex)
{
lblUserList.Text = ex.ToString();
}


}

How to update an Active Directory User in c#

// Create a DirectorySearcher object using the user name as the LDAP search filter. If using a directory other than Exchange, use sAMAccountName instead of mailNickname.
DirectorySearcher searcher = new DirectorySearcher("(cn=" + _UserName + ")");

// Search for the specified user.
SearchResult result = searcher.FindOne();

// Make sure the user was found.
Response.Write(_UserName);
// Create a DirectoryEntry object to retrieve the collection of attributes (properties) for the user.
DirectoryEntry user = result.GetDirectoryEntry();

domain = ConfigurationManager.AppSettings["DOMAIN"].ToString();

user.Username = domain + "\\" + ConfigurationManager.AppSettings["USERNAME"].ToString();
user.Password = ConfigurationManager.AppSettings["PASSWORD"].ToString();
user.AuthenticationType = AuthenticationTypes.Secure;



if (details[1] != "")
{
user.Properties["TelephoneNumber"].Value = details[1];
}

if (details[2] != "")
{
user.Properties["streetAddress"].Value = details[2];
}

if (details[3] != "")
{
user.Properties["Description"].Value = details[3];
}

if (details[4] != "")
{
user.Properties["mail"].Value = details[4];
}
if (details[8] != "")
{
user.Properties["sn"].Value = details[8];//last name
}
if (details[9] != "")
{
user.Properties["displayName"].Value = details[9];//display name
}
if (details[10] != "")
{
user.Properties["givenName"].Value = details[0];//1st name
}
if (details[11] != "")
{
user.Properties["physicalDeliveryOfficeName"].Value = details[11];//office phone
}
if (details[12] != "" && details[13] != "")
{
user.Properties["co"].Value = details[12];//country name
user.Properties["c"].Value = details[13];//country code
}

user.CommitChanges();

if (details[5] != "" & details[6] != "")
{
if (details[5].Equals(details[6]))
{
user.Invoke("SetPassword", new object[] { details[5] });
user.CommitChanges();

}
else
{
lblUserList.ForeColor = Color.Red;
lblUserList.Text = "Password Mismatch";
return false;
}
}

// Clean up.
searcher.Dispose();
result = null;
user.Close();