Tuesday, January 3, 2012

Current User Tasks Code using SharePoint Object Model...!

SPWeb currentWeb = SPControl.GetContextWeb(Context);
                    SPList lstTasks = currentWeb.Lists["Tasks"];
                    if (lstTasks != null)
                    {
                        SPQuery sQuery = new SPQuery();
                        sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy><Where><Eq><FieldRef Name='AssignedTo' /><Value Type='Integer'><UserID Type='Integer' /></Value></Eq></Where>";
                        SPListItemCollection tasksList = lstTasks.GetItems(sQuery);
                        writer.Write("<Table width='100%' style='" + strStyleValign + "'>");
                        if (tasksList.Count > 0)
                        {
                            writer.Write("<Tr>");
                            writer.Write("<Td colspan='3' align='Center'>");
                            writer.Write("<Strong>");
                            writer.Write("<Font color='Red'>");
                            writer.Write("Current User Tasks");
                            writer.Write("</Font>");
                            writer.Write("</Strong>");
                            writer.Write("</Td>");
                            writer.Write("</Tr>");
                            writer.Write("<Tr>");
                            writer.Write("<Td>");
                            writer.Write("Task Name");
                            writer.Write("</Td>");
                            writer.Write("<Td>");
                            writer.Write("Task Status");
                            writer.Write("</Td>");
                            writer.Write("<Td>");
                            writer.Write("Task Priority");
                            writer.Write("</Td>");
                            writer.Write("</Tr>");
                            foreach (SPListItem sQueryItem in tasksList)
                            {
                                string strTitle = string.Empty;
                                strTitle = sQueryItem["Title"].ToString();
                                string strStatus = string.Empty;
                                string strPriority = string.Empty;
                                string strUrl = currentWeb.Url + "/Lists/Tasks/DispForm.aspx?ID=" + sQueryItem.ID + "&source=" + Page.Request.Url;
                                writer.Write("<Tr>");
                                writer.Write("<Td>");
                                writer.Write("<A href='" + strUrl + "'>" + strTitle + "</A>");
                                writer.Write("</Td>");
                                writer.Write("<Td>");
                                if (sQueryItem["Status"] != null)
                                {
                                    strStatus = sQueryItem["Status"].ToString().Substring(sQueryItem["Status"].ToString().LastIndexOf("#") + 1);
                                }
                                else
                                {
                                    strStatus = "--";
                                }
                                writer.Write(strStatus);
                                writer.Write("</Td>");
                                writer.Write("<Td>");
                                if (sQueryItem["Priority"] != null)
                                {
                                    strPriority = sQueryItem["Priority"].ToString().Substring(sQueryItem["Priority"].ToString().LastIndexOf("#") + 1);
                                }
                                else
                                {
                                    strPriority = "--";
                                }
                                writer.Write(strPriority);
                                writer.Write("</Td>");
                                writer.Write("</Tr>");
                            }
                        }
                        else
                        {
                            writer.Write("<Tr>");
                            writer.Write("<Td colspan='3' align='Center'>");
                            writer.Write("There are no Active Tasks");
                            writer.Write("</Td>");
                            writer.Write("</Tr>");
                        }
                    }
                    else
                    {
                        writer.Write("<Tr>");
                        writer.Write("<Td colspan='3' align='Center'>");
                        writer.Write("Taks List Not available");
                        writer.Write("</Td>");
                        writer.Write("</Tr>");
                    }
                    writer.Write("</Table>");

No comments:

Post a Comment