Sunday, 22 February 2015

SharePoint 2010 Query to get List items assigned to current logged in user with Status

 Retrieve all list items assigned to current logged in user based on Status value


SPUser currentUser = SPContext.Current.Web.CurrentUser;

string name = currentUser.Name.ToString();

SPList list = web.Lists["Listname"];

SPQuery dataQuery= new SPQuery();

dataQuery.ViewFields = "<FieldRef Name='ID'/>";

dataQuery.Query = "<Where><And><Gt><FieldRef Name='ID' /><Value Type='Counter'>0</Value></Gt><And><Eq>"
+ "<FieldRef Name='UserName' /><Value Type='User'>"+name +"</Value></Eq><Or><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 1</Value></Eq><Or><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 2</Value></Eq><Eq>"
+ "<FieldRef Name='Status' /><Value Type='Choice'>Status 3</Value></Eq></Or></Or></And></And></Where>";



Cheers :)
 

SharePoint 2010 Programatically Upload document and update column of document library

In this post i am sharing learning about "Upload a document  through File Upload control to a SharePoint document library and update a document library too", Below is the code

private void UploadFile(string fileName, byte[] fileBytes)
        {
                using (SPSite site = SPContext.Current.Site)
                {
                    if (site != null)
                    {
                        using (SPWeb web = site.OpenWeb())
                        {
                            SPFolder docLibrary = web.Folders["DocumentLibraryName"];
                            if (docLibrary != null)
                            {
                                Boolean replaceExistingFiles = true;
                               
                                SPFile spFile = docLibrary.Files.Add(fileName, fileBytes, replaceExistingFiles);
                                spFile.Item["ColumnName"] = "Some information";
                               
                                spFile.Item.Update();
                                docLibrary.Update();
                            }
                        }
                    }
                }
            }

Cheers :)