private void Edit(Project pro)
        {
            if (oList != null)
            {
                // Edit
                if (pro.Id != 0)
                {
                    // Assume there is a list item with ID=1.
                    ListItem listItem = oList.GetItemById(pro.Id);
                    // Write a new value to the Body field of the Announcement item.
                    listItem["ProjectName"]     = pro.ProjectName;
                    listItem["ProjDescription"] = pro.Description;
                    listItem["StartDate"]       = pro.StartDate;
                    listItem["_EndDate"]        = pro.EndDate;
                    listItem["State"]           = pro.State;

                    // Leader
                    FieldLookupValue lookup = new FieldLookupValue();
                    lookup.LookupId    = 3;
                    listItem["Leader"] = lookup;

                    // Members
                    List <FieldLookupValue> lvList = new List <FieldLookupValue>();
                    lvList.Add(lookup);
                    lvList.Add(new FieldLookupValue()
                    {
                        LookupId = 1
                    });
                    listItem["Member"] = lvList;
                    listItem.Update();
                    context.ExecuteQuery();

                    context.ExecuteQuery();
                }
                // Add new item
                else
                {
                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    ListItem newItem = oList.AddItem(itemCreateInfo);
                    newItem["Title"]           = Guid.NewGuid();
                    newItem["ProjectName"]     = pro.ProjectName;
                    newItem["ProjDescription"] = pro.Description;
                    newItem["StartDate"]       = pro.StartDate;
                    newItem["_EndDate"]        = pro.EndDate;
                    newItem["State"]           = pro.State;
                    newItem.Update();

                    context.ExecuteQuery();
                }
            }
        }
        /*
         * Applies a value (aka metadata) to a text column for a given item in a document library
         * Parameters:
         * 'context' client context object returned from ConnectToSharePoint()
         * 'documentLibraryName' name of the document library
         * 'columnName' is the name of the column
         * 'value' is the string value to apply in the column for the itemId
         * 'itemId' is the item id of the document (row) in the document library. Use GetItemId() to find this.
         */
        public void ApplyTextColumnMetadataToSharePointFile(ClientContext context, string documentLibraryName, string columnName, string value, int itemId)
        {
            try
            {
                Web web  = context.Web;
                var list = web.Lists.GetByTitle(documentLibraryName);
                context.Load(list.Fields);
                context.ExecuteQuery();

                foreach (Field f in list.Fields)
                {
                    if (f.Title == columnName) //Found a match
                    {
                        string internalColumnName = f.InternalName;
                        Microsoft.SharePoint.Client.List oList = context.Web.Lists.GetByTitle(documentLibraryName);
                        ListItem oListItem = oList.GetItemById(itemId);
                        oListItem[internalColumnName] = value;
                        oListItem.Update();
                        context.ExecuteQuery();
                    }
                    else
                    {
                        //No match found
                    }
                }
            }
            catch (Exception ex)
            {
                //Handle exception
            }
        }
        public SP.FileCollection Attachments(SPListItem listItem, SPList list, object value)
        {
            if (value == null || !(bool)value)
            {
                return(null);
            }

            using (var clientContext = new SPContext(list.SPWebUrl, credentials.Get(list.SPWebUrl)))
            {
                SP.Web web = clientContext.Web;
                clientContext.Load(web);
                SP.List splist = clientContext.ToList(list.Id);
                clientContext.Load(splist);
                SP.ListItem splistItem = splist.GetItemById(listItem.Id);
                clientContext.Load(splistItem);
                clientContext.ExecuteQuery();

                SP.Folder listFolder = splistItem.ParentList.RootFolder;
                clientContext.Load(listFolder, f => f.ServerRelativeUrl);
                clientContext.ExecuteQuery();

                SP.Folder attachmentsFolder = web.GetFolderByServerRelativeUrl(listFolder.ServerRelativeUrl + "/attachments/" + splistItem.Id.ToString());
                clientContext.Load(attachmentsFolder);
                var attachments = attachmentsFolder.Files;
                clientContext.Load(attachments);
                clientContext.ExecuteQuery();

                return(attachments);
            }
        }
예제 #4
0
        //Delete Documents
        public string DeleteDocument(string RepoName, string DocId)
        {
            string msg;

            try
            {
                ClientContext ctx = new ClientContext(this.objAuth.SiteURL);
                ctx.RequestTimeout = 50000;
                ctx.Credentials    = this.GetO365Credentials(this.objAuth.UserName, this.objAuth.UserPassword);
                Web web = ctx.Web;
                Microsoft.SharePoint.Client.List oList = web.Lists.GetByTitle(RepoName);

                ListItem DocToDelete = oList.GetItemById(DocId);
                DocToDelete.DeleteObject();
                oList.Update();
                if (ctx.HasPendingRequest)
                {
                    ctx.ExecuteQueryAsync()
                    .Wait();
                }

                msg = "Document Removed from Repository";
            }
            catch (Exception ex)
            {
                return("Error:" + ex.Message);
            }
            return(msg);
        }
예제 #5
0
        public int UpdateEntry(EmployeeModel emp, string siteUrl, string ID)
        {
            try
            {
                using (MSC.ClientContext context = GetContext(siteUrl))
                {
                    MSC.List list = context.Web.Lists.GetByTitle("TIM_DailyAttendance");

                    MSC.ListItem listItem = null;

                    MSC.ListItemCreationInformation itemCreateInfo = new MSC.ListItemCreationInformation();
                    listItem = list.GetItemById(Convert.ToInt32(ID));

                    listItem["AttendanceDate"] = Convert.ToDateTime(emp.attendance_date).ToString("dd-MM-yyyy");
                    listItem["CheckinTime"]    = emp.checkin_time;
                    listItem["CheckoutTime"]   = emp.checkout_time;
                    listItem["Comment"]        = emp.comment;
                    listItem["EmpNo"]          = emp.empno;
                    listItem["Hours"]          = emp.hours;
                    listItem["EmpName"]        = emp.name;
                    listItem["EmpMail"]        = emp.office_email;
                    listItem.Update();
                    context.ExecuteQuery();
                }
            }
            catch (Exception ex)
            {
            }

            return(0);
        }
        /*
         * Applies a value (aka metadata) to a text column for a given item in a document library
         * Parameters:
         * 'context' client context object returned from ConnectToSharePoint()
         * 'documentLibraryName' name of the document library
         * 'columnName' is the name of the column
         * 'itemId' is the item id of the document (row) in the document library
         * 'listItemId' is the item id from the lookup list. Use an Id returned from GetLookupListItems() Dictionary.
         */
        public void ApplyLookupColumnMetadataToSharePointFile(ClientContext context, string documentLibraryName, string columnName, int itemId, int listItemId)
        {
            try
            {
                //Lookup column internal name using DisplayName
                Web web  = context.Web;
                var list = web.Lists.GetByTitle(documentLibraryName);
                context.Load(list.Fields);
                context.ExecuteQuery();

                foreach (Field f in list.Fields)
                {
                    if (f.Title == columnName) //Found a match
                    {
                        string internalColumnName = f.InternalName;

                        Microsoft.SharePoint.Client.List oList = context.Web.Lists.GetByTitle(documentLibraryName);
                        ListItem oListItem = oList.GetItemById(itemId);

                        //Credit to http://sharepoint.stackexchange.com/questions/8017/how-to-set-listitem-lookup-field-value
                        oListItem[internalColumnName] = listItemId;
                        oListItem.Update();
                        context.ExecuteQuery();
                    }
                    else
                    {
                        //No match found
                    }
                }
            }
            catch (Exception ex)
            {
                //Handle error
            }
        }
예제 #7
0
파일: Form1.cs 프로젝트: tulitas/visual
        private void button3_Click(object sender, EventArgs e)
        {
            string delete = textBox2.Text;

            ListItem oListItem = oList.GetItemById(delete);


            clientContext.ExecuteQuery();
        }
예제 #8
0
 static void Main(string[] args)
 {
     using (ClientContext context = new ClientContext("https://mySharePointServer/sites/MySite/"))
     {
         context.Credentials = new NetworkCredential("myUserName", "myPassword", "MYDOMAIN");
         SP.List  calendarList = context.Web.Lists.GetByTitle("Calendar");
         ListItem cListItem    = calendarList.GetItemById(2301);                     //This is one way to retrieve an item for update. You can also use a Caml Query
         context.Load(cListItem);
         cListItem["MetaInfo"] = "Categories:SW|Red Category\r\n";
         cListItem.Update();
         context.ExecuteQuery();
     }
 }
예제 #9
0
        private void Edit(Employee emp)
        {
            if (oList != null)
            {
                string lang = GetLanguage(emp.Languages);
                // Edit
                if (emp.Id != 0)
                {
                    // Assume there is a list item with ID=1.
                    ListItem listItem = oList.GetItemById(emp.Id);

                    // Write a new value to the Body field of the Announcement item.
                    listItem["EmailAdd"]             = emp.Email;
                    listItem["ShortDesc"]            = emp.ShortDescription;
                    listItem["FirstName"]            = emp.FirstName;
                    listItem["Title"]                = emp.Title;
                    listItem["ProgrammingLanguages"] = lang;
                    listItem.Update();

                    context.ExecuteQuery();
                }
                // Add new item
                else
                {
                    ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                    ListItem newItem = oList.AddItem(itemCreateInfo);
                    newItem["EmailAdd"]             = emp.Email;
                    newItem["ShortDesc"]            = emp.ShortDescription;
                    newItem["FirstName"]            = emp.FirstName;
                    newItem["Title"]                = emp.Title;
                    newItem["ProgrammingLanguages"] = lang;
                    newItem.Update();

                    context.ExecuteQuery();
                }
            }
        }
예제 #10
0
        private void btnShred_Click(object sender, EventArgs e)
        {
            SP.List docsList = web.Lists.GetByTitle("Documents");
            context.Load(docsList);

            for (int i = 0; i < listFiles.Items.Count; i++)
            {
                ListViewItem item = listFiles.Items[i];

                if (item.Checked)
                {
                    ListItem listItem = docsList.GetItemById(i);
                    listItem.DeleteObject();
                }
            }

            context.ExecuteQuery();
        }
예제 #11
0
        private void btndelete_Click(object sender, EventArgs e)
        {
            try
            {
                Int32 selectedRowCount = gridlist.Rows.GetRowCount(DataGridViewElementStates.Selected);

                if (selectedRowCount > 1)
                {
                    throw new Exception("Please select only one record");
                }
                else if (selectedRowCount == 1)
                {
                    string ID = gridlist.SelectedRows[0].Index.ToString();

                    string value = gridlist.SelectedRows[0].Cells[1].Value.ToString();

                    ClientContext clientContext = new ClientContext(siteurl + "//" + subsite);
                    SP.List       oList         = clientContext.Web.Lists.GetByTitle("Resource_Level");
                    ListItem      oListItem     = oList.GetItemById(value);

                    oListItem.DeleteObject();

                    clientContext.ExecuteQuery();

                    lblmessage6.Text      = "Data deleted succesfully";
                    lblmessage6.ForeColor = System.Drawing.Color.Green;
                    lblmessage6.Visible   = true;

                    btnlist.PerformClick();
                }
                else
                {
                    throw new Exception("Please select at least one record");
                }
            }
            catch (Exception ex)
            {
                lblmessage6.Text      = ex.Message;
                lblmessage6.ForeColor = System.Drawing.Color.Red;
                lblmessage6.Visible   = true;
            }
        }
        private void deleteButton_Click(object sender, RoutedEventArgs e)
        {
            string        UserName = "******", Password = "******";
            ClientContext ctx      = new ClientContext("https://omisayeduiu.sharepoint.com/sites/sayeddev");
            SecureString  passWord = new SecureString();

            foreach (char c in Password.ToCharArray())
            {
                passWord.AppendChar(c);
            }
            ctx.Credentials = new SharePointOnlineCredentials(UserName, passWord);

            Web myWeb = ctx.Web;

            Microsoft.SharePoint.Client.List sectionsList = myWeb.Lists.GetByTitle("sectionInfo");

            Microsoft.SharePoint.Client.ListItem item = sectionsList.GetItemById(Convert.ToInt32(deleteId.Text.Trim()));
            item.DeleteObject();
            ctx.ExecuteQuery();
            this.show();
        }
        public void UpdateItem(string ListName, TEntity Item)
        {
            try
            {
                SP.Web  web  = context.Web;
                SP.List list = web.Lists.GetByTitle(ListName);

                var         properties = typeof(TEntity).GetProperties();
                var         id         = properties.FirstOrDefault(f => f.Name == "ID");
                SP.ListItem item       = list.GetItemById(Convert.ToInt32(id.GetValue(Item)));
                if (item != null)
                {
                    foreach (var property in properties)
                    {
                        if (property.Name != "ID" && property.Name != "Created" && property.Name != "Author")
                        {
                            item[property.Name] = property.GetValue(Item);
                        }
                    }

                    item.Update();
                    context.Load(item);

                    context.ExecuteQuery();
                }
                else
                {
                    throw new Exception("Item does not exist");
                }
            }
            catch (InvalidCastException ex)
            {
                throw new Exception(ex.Message + ". Data model types are invalid, please verify that models are strongly typed and match the SharePoint list column types.", new InvalidCastException());
            }
            catch (Exception ex)
            {
                throw new Exception("Error updating SharePoint list data: " + ex.Message);
            }
        }
        public void DeleteItem(string ListName, int Id)
        {
            try
            {
                SP.Web  web  = context.Web;
                SP.List list = web.Lists.GetByTitle(ListName);

                SP.ListItem item = list.GetItemById(Id);
                if (item != null)
                {
                    item.DeleteObject();
                    context.ExecuteQuery();
                }
                else
                {
                    throw new Exception("Item does not exist");
                }
            }
            catch (Exception ex)
            {
                throw new Exception("Error updating SharePoint list data: " + ex.Message);
            }
        }
        public static void UpdateStatus(int ID, string siteUrl, string ListName)
        {
            using (MSC.ClientContext context = CustomSharePointUtility.GetContext(siteUrl))
            {
                if (context != null)
                {
                    MSC.List     list     = context.Web.Lists.GetByTitle(ListName);
                    MSC.ListItem listItem = null;

                    MSC.ListItemCreationInformation itemCreateInfo = new MSC.ListItemCreationInformation();
                    listItem = list.GetItemById(ID);

                    listItem["SystemUpdated"] = "Yes";
                    listItem.Update();
                    try
                    {
                        context.ExecuteQuery();
                    }
                    catch (Exception ex)
                    {
                    }
                }
            }
        }
예제 #16
0
        public ActionResult ScanTemplate()
        {
            var    spContext    = SharePointContextProvider.Current.GetSharePointContext(System.Web.HttpContext.Current);
            string spListId     = this.Request["SPListId"];
            string spListItemId = this.Request["SPListItemId"];
            string relativeUrl  = this.Request["url"];

            using (var clientContext = spContext.CreateUserClientContextForSPHost())
            {
                SP.File file = null;

                SP.Web web = clientContext.Web;
                clientContext.Load(web);

                if (string.IsNullOrEmpty(relativeUrl))
                {
                    Guid listId = new Guid(spListId);
                    int  itemId = Convert.ToInt32(spListItemId);

                    SP.List spList = clientContext.Web.Lists.GetById(listId);
                    clientContext.Load <SP.List>(spList);

                    SP.ListItem item = spList.GetItemById(itemId);
                    clientContext.Load <SP.ListItem>(item);
                    clientContext.ExecuteQuery();
                    file = item.File;
                }
                else
                {
                    file = web.GetFileByServerRelativeUrl(relativeUrl);
                }
                FormInformation info = InfoPathAnalytics.FormInformation(clientContext, file);
                info.XsnUrl = file.ServerRelativeUrl;
                return(View(info));
            }
        }
예제 #17
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnUpdateItem_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                SPClient.List announcementsList = ctx.SPContext.Web.Lists.GetByTitle("MyFirstList");

                ListItemCreationInformation itemCreateInfo = new ListItemCreationInformation();
                ListItem listItem = announcementsList.GetItemById(this.txtBoxId.Text);

                listItem["Description"] = this.txtBoxDescriptionUpdate.Text;
                listItem.Update();

                ctx.SPContext.ExecuteQuery();

                MessageBox.Show("Item inserted");

                this.txtBoxId.Text = "";
                this.txtBoxDescriptionUpdate.Text = "";
            }
            catch (Exception ex)
            {
                RaiseException(ex);
            }
        }
예제 #18
0
        public static string GetItemByID(string listName, int ID)
        {
            string result = string.Empty;

            if (clientContext == null)
            {
                clientContext = new ClientContext(Proxy.SelectedServiceUri + Proxy.WebSite);
            }
            using (clientContext)
            {
                //获取列表
                Microsoft.SharePoint.Client.List oList = clientContext.Web.Lists.GetByTitle(listName);
                //通过ID获取Item
                ListItem listItem = oList.GetItemById(ID);

                //加载
                clientContext.Load(listItem);
                //执行
                clientContext.ExecuteQuery();

                #region 获取指定ID文档下的所有文件名称

                clientContext.Load(oList.RootFolder.Folders);
                clientContext.ExecuteQuery();

                Folder folderGet = null;

                foreach (var item in oList.RootFolder.Folders)
                {
                    if (item.Name.Equals(Proxy.attch))
                    {
                        clientContext.Load(item.Folders);
                        clientContext.ExecuteQuery();

                        foreach (var itemChild in item.Folders)
                        {
                            if (itemChild.Name.Equals(ID.ToString()))
                            {
                                folderGet = itemChild;
                                break;
                            }
                        }
                        break;
                    }
                }

                if (folderGet != null)
                {
                    clientContext.Load(folderGet.Files);
                    clientContext.ExecuteQuery();

                    foreach (var item in folderGet.Files)
                    {
                        var fileName = item.ServerRelativeUrl.Substring(item.ServerRelativeUrl.LastIndexOf('/') + 1);
                        result += fileName + "," + Proxy.SelectedServiceUri + item.ServerRelativeUrl + ";";
                    }
                }

                #endregion
            }
            return(result);
        }