Prasad Bolla's SharePoint Blog

Click Here to go through the Interesting posts within my Blog.

Click Here to go through the new posts in my blog.

Monday, April 23, 2012

How to pass Current Logged User value into SharePoint Created By Column while insertion into SharePoint List.


SPSecurity.RunWithElevatedPrivileges(delegate()
            {
            SPWeb currentWeb = SPContext.Current.Web;   
            SPList lst = currentWeb.Lists["Sample"];
                SPListItemCollection myColl = lst.Items;
                SPListItem item = myColl.Add();
                item["Title"] = txtTitle.Text;   
                SPFieldUserValue value = new SPFieldUserValue(currentWeb, currentWeb.CurrentUser.ID, currentWeb.CurrentUser.LoginName);
                item["Author"] = value.LookupId;
                item.Update();

            });

Friday, April 13, 2012

How to check if the Logged user is in Multiple Groups....!

try
            {
                writer.Write("<Table width='100%'>");
                writer.Write("<Tr>");
                writer.Write("<Td Align='Center'>");
                writer.Write("<Strong>");
                writer.Write("How to check if the current logged user is in Multiple Groups...!");
                writer.Write("</Strong>");
                writer.Write("<Td>");
                writer.Write("</Tr>");
                bool strCurrentUser = false;
                SPSecurity.RunWithElevatedPrivileges(delegate()
                {
                    SPWeb currentWeb = SPContext.Current.Web;
                    SPGroupCollection grp = currentWeb.Groups;
                    foreach (SPGroup group in grp)
                    {
                        if (group.ID == 5)
                        {
                            strCurrentUser = true;
                        }
                    }
                    if (strCurrentUser == false)
                    {
                        writer.Write("<Tr>");
                        writer.Write("<Td>");
                        writer.Write("<Strong>");
                        writer.Write("Current user is not an Administrator...!");
                        writer.Write("</Strong>");
                        writer.Write("<Td>");
                        writer.Write("</Tr>");
                    }
                    else
                    {
                        writer.Write("<Tr>");
                        writer.Write("<Td>");
                        writer.Write("<Strong>");
                        writer.Write("Current User is an Administrator");
                        writer.Write("</Strong>");
                        writer.Write("<Td>");
                        writer.Write("</Tr>");
                    }

                });
                writer.Write("</Table>");

            }
            catch (Exception ex)
            {

                writer.Write(ex.ToString());
            }


Saturday, March 31, 2012

Binding Data from SharePoint Links List to SpGridView without a single line of loop.

ascx
<table width="100%" cellpadding="0" cellspacing="0">
<tr>
<td>Binding Data from SharePoint Links List to SpGridView without a Single line of Loop.</td>
</tr>
<tr>
<td>
<SharePoint:SPGridView ID="sgvLinks" runat="server" AutoGenerateColumns="false" DataKeyNames="ID">
<Columns>
<asp:BoundField HeaderText="Link Name" DataField="URL" />
</Columns>
</SharePoint:SPGridView>
</td>
</tr>
</table>


Ascx.cs
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using System.Security;
namespace DLLWebPart.DLLWebPart
{
    public partial class DLLWebPartUserControl : UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
                sgvLinks.RowDataBound += new GridViewRowEventHandler(sgvLinks_RowDataBound);
                SPWeb currentWeb = SPContext.Current.Web;
                SPList LinksList = currentWeb.Lists["Links"];
                SPQuery sQuery = new SPQuery();
                sQuery.Query = "<OrderBy><FieldRef Name='ID' Ascending='False' /></OrderBy>";
                sQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='URL' />";
                SPListItemCollection myColl = LinksList.GetItems(sQuery);
                if (myColl.Count > 0)
                {
                    if (!IsPostBack)
                    {
                        sgvLinks.DataSource = myColl.GetDataTable();
                        sgvLinks.DataBind();
                    }
                }
        }
        string strItemID = "";
        void sgvLinks_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    SPSecurity.RunWithElevatedPrivileges(delegate()
                    {
                        strItemID = sgvLinks.DataKeys[e.Row.RowIndex].Value.ToString();
                        SPWeb currentWeb = SPContext.Current.Web;
                        SPList lst = currentWeb.Lists["Links"];
                        SPQuery sQuery = new SPQuery();
                        sQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>"+strItemID+"</Value></Eq></Where>";
                        sQuery.ViewFields = "<FieldRef Name='ID' /><FieldRef Name='URL' />";
                        SPListItemCollection myColl = lst.GetItems(sQuery);
                        if (myColl.Count > 0)
                        {
                            SPListItem item = myColl[0];
                            SPFieldUrlValue URL = new SPFieldUrlValue(item["URL"].ToString());
                            e.Row.Cells[0].Text = "<a href='"+URL.Url+"'>"+URL.Description+"</a>";
                       
                        }
                    });
                }
            }
            catch (Exception ex)
            {

                Response.Write(ex.ToString());
            }
        }
    }
}