Wednesday, December 22, 2010

Linq to class objects

This example shows how to use the array of class object and its variables on run time using "LINQ".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
public class LinqClass
{
    public string Name;
    public string contact;
    public string city;

}

using System.Xml;
using System.Xml.Linq;
using System.Linq;
using System.Data;

var sp = new[] {
          new LinqClass { Name="Murugesan",
                          contact="9769104815",
                          city="Chennai"
                        },
                        new LinqClass { Name="Pandian",
                                        city="Madurai",
                                        contact="9786466642"
                                      }
          };
        foreach (var n in sp)
        { 
            Response.Write(n.Name.ToString());
        }

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