c# setting xmlns root attribute serialization

İf you want to  set xmlns attribute when serialization process you can  use XmlRoot attribute namespace .


[XmlRoot(Namespace= @”http://schemas.microsoft.com/sharepoint/”) ]

public class Elements


public Elements()



private string _ID;


public string ID


get { return _ID; }

set { _ID = value; }



happy tips..

c# LDAP can not Reset Password [SOLVED]

Hi Everyone ,

When i am working with LDAP noticed that the resetting password is very problematic . Finally i have found a solution.First of all you should configure your  activedirectory server with ssl authentication i’m not sure it isnecessary
but too many blogs and forums say to do that. Well ,to do so 🙂

And use this code to reset password in your client application

public static void ResetPassword(string dn,string userCN,string newpassword, string admin,string adminpass)
    //Create an active directory context
    DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain,
        admin,//you need admin privs.
    Domain objDomain = Domain.GetDomain(objContext);
    DirectoryEntry de = objDomain.GetDirectoryEntry();
    //geting user password
    DirectoryEntry passUser = de.Children.Find("OU=PORTAL").Children.Find("CN=" + userCN);
    using (passUser)
        //this is the common error line if you do not set correct configuration
        passUser.Invoke("SetPassword", new object[] { newpassword });
        passUser.CommitChanges();//don't forget to commint

Usage is simple:

ResetPassword(“testdomain.umbrellacorp.local”, “MyTestUser”, “secret”, “testdomain\\admin”, “adminsecret”);

Someone who encounter with “Invoke SetPassword” error that possible forget to use DirectoryContext class.
DirectoryContext is the key point .This class can access server with appropriate connection settings.You dont need to
set any client configuration for example port,protocol etc.

For more information with DirectoryContext

Happy codding …

c# LDAP Delete Active Directory user

UserDn : ldap path of the user account which is gonna delete.
admin : Priviledged user to delete mean admin account
adminpass : you know what is this 🙂



public static void DeleteUser(string userDn, string admin, string adminpass)
        DirectoryEntry user = new DirectoryEntry(userDn, admin, adminpass);
    catch (System.DirectoryServices.DirectoryServicesCOMException ex)

Sharepoint set user or group to SPListItem

We are going to use a sharepoint web control to get user or groups  named “PeopleEditor” .For using these please fallow the instructions below.

Add fallowing code to top of your page:

<%@ Register Tagprefix="SharePointWebControls" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>

Add fallowing control to your page front for get users or groups:

Note: If  you want to set or retrieve the account types that are associated with the control use attribute “SelectionSet”
and enums are:

  • User = single user
  • DL = AD distribution list
  • SecGroup = AD security group
  • SPGroup = Sharepoint group

<SharePointWebControls:PeopleEditor runat=”server” ID=”txtScope” MultiSelect=”false” MaximumEntities=”1″ ValidatorEnabled=”true” SelectionSet=”User” Width=”300px”></SharePointWebControls:PeopleEditor>

Add fallowing to your page behind:

using Microsoft.SharePoint.WebControls;
public SPFieldUserValueCollection GetSelectedUsers(PeopleEditor editor)
       string selectedUsers = editor.CommaSeparatedAccounts;
       // commaseparatedaccounts returns entries that are comma separated. we want to split those up
       char[] splitter = { ',' };
       string[] splitPPData = selectedUsers.Split(splitter);
       // this collection will store the user values from the people editor which we'll eventually use
       // to populate the field in the list
       SPFieldUserValueCollection values = new SPFieldUserValueCollection();
       // for each item in our array, create a new sp user object given the loginname and add to our collection
       for (int i = 0; i < splitPPData.Length; i++)
                string loginName = splitPPData[i];
                if (!string.IsNullOrEmpty(loginName))
                    SPUser user = SPContext.Current.Web.SiteUsers[loginName];
                    SPFieldUserValue fuv = new SPFieldUserValue(SPContext.Current.Web, user.ID, user.LoginName);
            return values;

And now i describe add to listitem and update:

SPList list = SPContext.Current.Web.Lists[APPLIB];
SPListItem item = list.Items.Add();
item["ProgramAdmin"] = GetSelectedUsers(txtScope);

That’s all.

c# LDAP create new active directory user

public static string CreateUserAccount(string ldapPath, string userName, string userPassword)
    string oGUID = string.Empty;
        //    for example an ldap path : 
        string connectionPrefix = "LDAP://" + ldapPath;
        DirectoryEntry dirEntry = new DirectoryEntry(connectionPrefix, "TESTDOMAIN\\admin", "secret");
        DirectoryEntry newUser = dirEntry.Children.Add("CN=" + userName, "user");
        newUser.Properties["samAccountName"].Value = userName;
        oGUID = newUser.Guid.ToString();

        //If you dont have an SSL connection you can not set password
        newUser.Invoke("SetPassword", new object[] { userPassword });
        newUser.Properties["LockOutTime"].Value = 0;

        //Enable user
        int val = (int)newUser.Properties["userAccountControl"].Value;
        newUser.Properties["userAccountControl"].Value = val & ~0x2;


    catch (System.DirectoryServices.DirectoryServicesCOMException E)
        //DoSomethingwith --> E.Message.ToString();

    return oGUID;

Get URL without QueryString in ASP.NET

Is there an easy way to get the url part without the query string of a given Url?

Yeap .

string  sample = Request.Url.GetLeftPart(UriPartial.Path);

Or long way:

string stringUri = “http://blog.bugrapostaci.com/test.aspx?lang=en&#8221;;
Uri uri = new Uri(stringUri);
string query = uri.Query;
string url = stringUri.Substring(0, stringUri.Length – query.Length);

happy tips.bye.