Thursday, December 09, 2010

C# Writing XML strings on runtime

This is an example for creating the XML string from the dynamic data.

StringWriter sw = new StringWriter();
XmlTextWriter txtWriter = new XmlTextWriter(sw);
txtWriter.Formatting = Formatting.Indented;
//txtWriter.WriteStartDocument();
txtWriter.WriteStartElement("Employee");
DataSet myDataSet = new DataSet();
myDataSet.ReadXml("C:\\ContactXML\\Contacts.xml");
foreach (DataRow row in myDataSet.Tables[0].Rows)
{
txtWriter.WriteElementString("Name", row[0].ToString());

}
txtWriter.WriteEndElement();

//txtWriter.WriteEndDocument();
sw.Close();
string s1 = sw.ToString().TrimStart('{');
string s2 = s1.TrimEnd('}');

Monday, December 06, 2010

SharePoint-Active Directory Synchronize


Please do not follow the logic that retrieving user details from the Active Directory mentioned here, please visit my TechNet article for alternate approaches

Last week,I prepared the small SharePoint utility to retrieving the Active Directory users and synchronize with SharePoint List.



public string LDAPUsers()
        {
            string listD = "";
            string strResult = "";
            string strRes = "";
            DirectoryEntry entry = new DirectoryEntry("LDAP://domainName", "userid", "pAsSwrd");
            DirectorySearcher mySearcher = new DirectorySearcher(entry);
            mySearcher.Filter = "((objectCategory=Person))";

            foreach (SearchResult resEnt in mySearcher.FindAll())
            {
                listD = listD + resEnt.GetDirectoryEntry().Name + ",";


            }
            string[] test = listD.Split('=');
            for (int i = 0; i < test.Length; i++)
            {
                strResult = strResult + test[i].ToString();
            }

            string[] test1 = strResult.Split(',');
            for (int j = 0; j < test1.Length; j++)
            {
                strRes = strRes + test1[j] + ",";
            }
            return strRes.Replace("CN", ""); ;
        }
Below code pushing the string array values in to SharePoint List.
SPSite site = new SPSite("http://moss:4100");
        SPWeb web = site.OpenWeb();
        SPList list = web.Lists["LDAP User"];
        string[] names = LDAPUsers().Split(',');
        for (int j = 0; j < names.Length; j++)
        {
            SPListItem item = list.Items.Add();
            web.AllowUnsafeUpdates = true;
            item["Title"] = names[j].ToString();
            web.AllowUnsafeUpdates = true;
            item.Update();
            web.AllowUnsafeUpdates = false;
        }

Sunday, November 21, 2010

SPQuery Value retreiving

 SPSite site = new SPSite("http://localhost");
        SPWeb web = site.OpenWeb();
    
            SPList item = web.Lists["Friends"];

            SPQuery spQuery = new SPQuery();
            spQuery.Query = "" +
                "Senthil";
            SPListItemCollection ListItems = item.GetItems(spQuery);

            foreach (SPListItem items in ListItems)
            {
                Response.Write((items["Name"].ToString()) +
                    "
"); } }

Monday, November 08, 2010

UserProfileManager - Webservice in Sharepoint

Listing all domain users in Sharepoint 2007 using UserProfileManager webservice class.
First create the proxy class using wsdl or  add webreference from Visual Studio.
The main difference in using webservice method is any user can access the resources within default network credentials.
But Sharepoint object model lets you use only with in production server with administrative privileges.

 localhost.UserProfileService service = new localhost.UserProfileService();
        service.Credentials = System.Net.CredentialCache.DefaultCredentials;
        int a = (int)service.GetUserProfileCount();
        for (int i = 0; i < a; i++)
        {
            Response.Write(service.GetUserProfileByIndex(i).UserProfile[1].Values[0].Value);
        }