Tuesday, September 17, 2013

Retrieving attachments in SharePoint list Item

Retrieving the SharePoint List Item through OM model requires little tweaked. So you will get the meta data of your attached files in the specific Item. Code goes here.I am trying to retrieve the attached files from the First Item in the List.
public List GetAttachments(int itemId)
        {
            List fNAme = new List();
            using (SPSite site = new SPSite(SPContext.Current.Site.ID))
            {
                using (SPWeb web = site.OpenWeb())
                {
                    string sp = string.Empty;
                    var list = web.Lists["Case List"];
                    SPListItem item = list.GetItemById(1);
                    SPAttachmentCollection cols = item.Attachments;
                    SPFolder folder = web.Folders["Lists"].SubFolders[list.Title].SubFolders["Attachments"].SubFolders[item.ID.ToString()];
                    FileDetails f = null;
                    foreach (SPFile file in folder.Files)
                    {
                        f = new FileDetails();
                        f.FileName = file.Name;
                        f.FUrl = web.Url + file.ServerRelativeUrl;
                        SPUser user = file.ModifiedBy;
                        f.ModifiedBy = user.Name;
                        f.FVersion = file.MajorVersion.ToString();
                        f.fIcon = file.IconUrl;
                        fNAme.Add(f);
                    }

                }

            }
            return fNAme;
        }
       
Get and Set class for the File Details
public class FileDetails
    {
       public string FileName { get; set; }
       public string ModifiedBy { get; set; }
       public string FUrl { get; set; }
       public string FVersion { get; set; }
       public string fIcon { get; set; }
    }

Tuesday, September 10, 2013

DataTable to CSV generator

This code will generate the .csv (Comma separator file) from the Data source like DataTable or XML strings.
    protected void CreateCSV(object sender,EventArgs e)
        {
            MemoryStream ms = new MemoryStream();
            DataSet ds = new DataSet();
            ds.ReadXml(@"d:\students.xml");
            if (ds.Tables[0].Rows.Count > 0)
            {
                StreamWriter writer = new StreamWriter(ms);
                writer.AutoFlush = true;

                foreach (DataColumn column in ds.Tables[0].Columns)
                {
                    writer.Write(column.ColumnName.ToString() + ",");
                }
                writer.WriteLine();

                foreach (DataRow drow in ds.Tables[0].Rows)
                {
                      writer.Write(drow["Name"].ToString() + "," + Convert.ToDateTime(drow["DOJ"]).ToShortDateString().ToString() + "," + drow["Height"].ToString() + ",");
                      writer.WriteLine();
                }
            }
            using (FileStream file = new FileStream("D:\\file5.csv", FileMode.Create, FileAccess.Write))
            {
                
                ms.WriteTo(file);
            }
        }
Sample XML file to work on the above solution.


Murugesan
12/12/2013
155.57845645


Murugesan
12/12/2013
155.57845645


Amit Ingle
05/04/2013
155.57845645


Monday, September 02, 2013

Removing SPWeb Property Value

Removing the Property Bag in the SharePoint 2010 is just little bit tricky.When I started to update the SPWeb object's property's properties.Remove and Update won't work. You need to set your Property Bag's value null before remove and update method called. Another important point is you have to call the remove method of AllProperties collection of SPWeb Object in SharePoint Object Model. It was different when I tested it with Power Shell Script in there I don't want to set the value null. Adding value to Property bag
web.Properties.Add("SomeValue", "900");
web.Properties.Update();
web.Update();
Removing the value and PropertyBag from the SPWeb's PropertyCollection.
web.Properties["SomeValue"] = null; // This is mandatory
web.Properties.Update();
web.AllProperties.Remove("SomeValue");
web.Properties.Update();
Its something like we can't remove the directory from the DOS command until we delete all of its files.