📜 ⬆️ ⬇️

Making familiar with the SharePoint 2010 list item

The principle of operation is quite simple. When viewing a list item, the user sees the button Read after clicking on which data about him and the time of clicking fall into another list where they will be stored. This list will be automatically created after the web-desk is placed on the item's viewing page, and its name will be generated in the following way: ReadList_ <Guid list for familiarization>. To view the lists of familiar and non-acquainted, we will write another web part which we will place on the list editing form which we will read, since it is assumed that ordinary users can view, but not edit the document.

Here is what the web desk looks like.
Before pressing the button:
image

And after clicking:
image
We create the actual list itself, the elements of which users should familiarize themselves with. I called it TestReadList , we add an additional field. Assigned to the type of the field. Person or Group.
In Visual Studio, we create an empty SharePoint2010 SPConfirmReadProject project.
Add the Visual Web Part to the project, let's call it ConfirmWebPart, also add a link to the SharePoint SharePoint folder of the project (Right button on the project. Add-> SharePoint “Images” Mapped Folder) then add your own button picture to the Images \ SPConfirmReadProject folder. It will be used on LinkButton.
Here is the ConfirmWebPart.ascx code
<table> <tr> <td> <asp:LinkButton ID="bRead" runat="server" OnClick="bRead_Click"><img src="../_Layouts/Images/SPConfirmReadProject/accept.png" style="border:0px;"/></asp:LinkButton> </td> </tr> <tr> <td> <asp:Label ID="lStatus" runat="server" Text=""></asp:Label> </td> </tr> </table> 

Here we create LinkButton and Label.

Go to the ConfirmWebPart.asxc.cs code:
 protected void Page_Load(object sender, EventArgs e) { bool isReal = false; //        . foreach (SPList list in SPContext.Current.Web.Lists) { if (list.Title == "ReadList_" + SPContext.Current.ListId) { isReal = true; } } SPList listConfirmReadUser; //      if (isReal) listConfirmReadUser = SPContext.Current.Web.Lists["ReadList_"+SPContext.Current.ListId]; //     else { SPContext.Current.Web.Lists.Add("ReadList_" + SPContext.Current.ListId, "", SPListTemplateType.GenericList); listConfirmReadUser = SPContext.Current.Web.Lists["ReadList_" + SPContext.Current.ListId]; listConfirmReadUser.Fields.Add("User", SPFieldType.User, true); listConfirmReadUser.Fields.Add("Date", SPFieldType.DateTime, true); listConfirmReadUser.Fields.AddLookup("ListItem", SPContext.Current.ListId, true); listConfirmReadUser.Update(); } //         SPListItemCollection listItems = listConfirmReadUser.Items; foreach (SPListItem item in listItems) { string user = item["User"].ToString().Split('#')[1]; string listItem = item["ListItem"].ToString().Split('#')[0]; if ((listItem.Remove(listItem.Length-1) == SPContext.Current.ItemId.ToString()) && (user.ToString() == SPContext.Current.Web.CurrentUser.Name.ToString())) { //          . bRead.Visible = false; lStatus.Text = "    " + item["Date"].ToString(); } } } //          protected void ConfirmUser() { var listConfirmReadUser = SPContext.Current.Site.RootWeb.Lists["ReadList_" + SPContext.Current.ListId]; SPListItemCollection listItems = listConfirmReadUser.Items; SPListItem item = listItems.Add(); item["Date"] = DateTime.Now; item["ListItem"] = SPContext.Current.ItemId; item["User"] = SPContext.Current.Web.CurrentUser; item["Title"] = ": " + SPContext.Current.Web.CurrentUser.ToString() + " - " + DateTime.Now; item.Update(); bRead.Visible = false; lStatus.Text = "    " + item["Date"].ToString(); } protected void bRead_Click(object sender, EventArgs e) { ConfirmUser(); } 

')
Everything can now be deployed. Web desk is placed on the form of viewing the element.
To do this, on the List tab of the ribbon, select Form Web Parts -> Default Display Form as shown in the figure.
image
In the form window, click Add a Web Part. In Categories, we’ll select Custom, and in Web Parts we’ll have our web desk, it’s called SPConfirmReadProject - ReadUsersWebPart. Click the Add and Stop Editing buttons.

That's actually all on the web-desk review.

Now we will write a web-desk view who is familiar and who is not.
The web desk consists of 2 LinkButton, Label and GridView.
Here is how it looks when you click on the "With the document read."
image
Everything is just we stupidly climb into the list of familiar and pull them out. And the interesting thing happens if we click on the button “Not familiarized”.
image
For this, the “Assigned to” field is used, which lists the people and / or groups that are assigned to read the item. To solve this problem, we take a list of people from the field “Assigned to” and compare it with people from the list of those who have read. Let's get started

Add a VisualWebPart to the project. Let's call it ReadUsersWebPart .
Add to ReadUsersWebPart.ascx
 <table> <tr> <td> <asp:LinkButton ID="lbRead" runat="server" OnClick="lbRead_Click" BorderWidth="0"><img src="../_Layouts/Images/SPConfirmReadProject/thumb_up.png" style="border:0px;" />  </asp:LinkButton> </td> <td> <p> </p> </td> <td> <asp:LinkButton ID="lbUnRead" runat="server" OnClick="lbUnRead_Click" BorderWidth="0"><img src="../_Layouts/Images/SPConfirmReadProject/thumb_down.png" style="border:0px;" /> </asp:LinkButton> </td> </tr> </table> <p></p> <asp:Label ID="Label1" runat="server"></asp:Label> <asp:GridView ID="gridView" runat="server" AllowPaging="True" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" EnableModelValidation="True" ForeColor="Black" GridLines="Horizontal" PageSize="20"> <FooterStyle BackColor="#CCCC99" ForeColor="Black" /> <HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" /> <SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" /> </asp:GridView> 


And in ReadUsersWebPart.ascx.cs we write the following:
 private SPGroup SearchGroup(string group) { SPGroup groupObject = null; foreach (SPGroup singleGroup in SPContext.Current.Web.Groups) { if (group == singleGroup.Name) { groupObject = singleGroup; } } return groupObject; } //   protected void ReadUsers() { var table = new DataTable("ReadList"); var colFIO = new DataColumn(); colFIO.DataType = System.Type.GetType("System.String"); colFIO.ColumnName = ""; table.Columns.Add(colFIO); var colDate = new DataColumn(); colDate.DataType = System.Type.GetType("System.DateTime"); colDate.ColumnName = ""; table.Columns.Add(colDate); string idItem = SPContext.Current.ItemId.ToString(); string idUser = SPContext.Current.Web.CurrentUser.ID.ToString().Split('#')[0].ToString().Replace(";",""); var listConfirmReadUser = SPContext.Current.Web.Lists["ReadList_" + SPContext.Current.ListId]; var query = new SPQuery(); string calmQuery = "<Where><And><Eq><FieldRef Name='ListItem' LookupId='true'/><Value Type='Lookup'>" + idItem + "</Value></Eq><Eq><FieldRef Name='User' LookupId='true'/><Value Type='User'>" + idUser + "</Value></Eq></And></Where>"; query.Query = calmQuery; SPListItemCollection listItems = listConfirmReadUser.GetItems(query); if (listItems.Count > 0) foreach (SPListItem item in listItems) { DataRow row; row = table.NewRow(); row[""] = item["User"].ToString().Split('#')[1]; row[""] = item["Date"]; table.Rows.Add(row); } Label1.Text = ""; gridView.DataSource = table; gridView.DataBind(); if (table.Rows.Count < 1) { Label1.Text = "   ."; } } //    protected void UnReadUsers() { string idItem = SPContext.Current.ItemId.ToString(); string idUser = SPContext.Current.Web.CurrentUser.ID.ToString().Split('#')[0].ToString().Replace(";", ""); string currentValue = string.Empty; string FieldName = string.Empty; try { currentValue = SPContext.Current.Item[" "].ToString(); FieldName = " "; } catch { currentValue = SPContext.Current.Item["Assigned to"].ToString(); FieldName = "Assigned to"; } List<SPUser> listAllUser = new List<SPUser>(); SPFieldUser UsersColumn = (SPFieldUser)SPContext.Current.Fields.GetField(FieldName); SPFieldUserValueCollection Users = (SPFieldUserValueCollection)UsersColumn.GetFieldValue(SPContext.Current.Item[FieldName].ToString()); foreach (SPFieldUserValue user in Users) { SPGroup group = SearchGroup(user.LookupValue); if (group != null) { SPUserCollection groupUsers = group.Users; foreach (SPUser groupUser in groupUsers) { if (groupUser != null) listAllUser.Add(groupUser); } } else { if (user.User != null) listAllUser.Add(user.User); } } var query = new SPQuery(); string calmQuery = "<Where><And><Eq><FieldRef Name='ListItem' LookupId='true'/><Value Type='Lookup'>" + idItem + "</Value></Eq><Eq><FieldRef Name='User' LookupId='true'/><Value Type='User'>" + idUser + "</Value></Eq></And></Where>"; query.Query = calmQuery; SPList list = SPContext.Current.Web.Lists["ReadList_" + SPContext.Current.ListId]; SPListItemCollection listReadUsers = list.GetItems(query); if (listReadUsers.Count > 0) { List<SPUser> listReadUser = new List<SPUser>(); foreach (SPListItem item in listReadUsers) { SPFieldUser UserReadColumn = (SPFieldUser)item.Fields.GetField("User"); SPFieldUserValue ur = (SPFieldUserValue)UserReadColumn.GetFieldValue(item["User"].ToString()); listReadUser.Add(ur.User); } if (listReadUser.Count() > 0) foreach (SPUser rUser in listReadUser) { if (rUser != null) listAllUser.RemoveAll(u => u.ID == rUser.ID); } } List<SPUser> usd = new List<SPUser>(); foreach (SPUser spUser in listAllUser) { List<SPUser> listUsr = (from u in listAllUser where (u.ID == spUser.ID) select u).ToList(); if (listUsr.Count() > 1) { usd.Add(listUsr[0]); } } foreach (SPUser spUser in usd) { listAllUser.Remove(spUser); } var table = new DataTable("UnReadList"); var colFIO = new DataColumn(); colFIO.DataType = System.Type.GetType("System.String"); colFIO.ColumnName = ""; table.Columns.Add(colFIO); foreach (SPUser spUser in listAllUser) { DataRow row; row = table.NewRow(); row[""] = spUser.Name; table.Rows.Add(row); } Label1.Text = ""; gridView.DataSource = table; gridView.DataBind(); if (listAllUser.Count() < 1) { Label1.Text = "   ."; } } protected void lbRead_Click(object sender, EventArgs e) { ReadUsers(); } protected void lbUnRead_Click(object sender, EventArgs e) { UnReadUsers(); } 

Then you can send the mailing to users who are registered in the field Assigned to using Workflow in Visual Studio or SharePoint Designer.

I hope the article will be useful to you. Soon I will write how you can print these lists, and for one and how to write Workflow mailings for this task in VS.

Source: https://habr.com/ru/post/192506/


All Articles