Search This Blog

Tuesday, March 29, 2016

How to find current user has edit permissions in sharepoint 2013 programmatically?

//Get the current loged in user
 SPUser currentUser = SPContext.Current.Web.CurrentUser;

 using (SPSite site = new SPSite("http:\\servername\TestSite"))
 {
    site.AllowUnsafeUpdates = true;
    using (SPWeb web = site.OpenWeb())
    {
        SPList oList = web.Lists["Test"];
        if(oList!=null)
        {
           SPListItemCollection collListItems=lstAPAs.GetItems();
           if(collListItems!=null)
           {
               foreach (SPListItem oListItem in collListItems)
               {
                    bool Ispermission = Convert.ToBoolean(oListItem.DoesUserHavePermissions                                (currentUser, SPBasePermissions.EditListItems));
                   if(Ispermission)
                   {
                       //Current user has edit permission for the list item
                   }
                   else
                   {
                      //Current User doen't have permission for the list item
                   }
               }
          }
         }
         site.AllowUnsafeUpdates = false;
    }
  }

Wednesday, March 23, 2016

How to find the ListItemId is exist or not in Sharepoint List Programatically?



                using (SPSite oSite = new SPSite("http:\\serverName\site\TestProject"))
                {
                    using (SPWeb oWeb = oSite.OpenWeb())
                    {
                        SPList oList = oWeb.Lists.TryGetList("TestList");
                        if (oList != null)
                        {
                            var listEnumeration = oList.Items.OfType<SPListItem>();
                            bool IsExist = listEnumeration.Any(p => p.ID == 1);
                            if (IsExist)
                            {
                                //List Item id exists in List
                            }
                            else
                            {
                                //List Item id doesn't exists in List
                            }
                        }

                    }
                 }

Wednesday, March 16, 2016

Programmatically close dialogue box in share point

private void CloseDialogPopup()
{
    try
   {
      HttpContext context = HttpContext.Current;
      if (HttpContext.Current.Request.QueryString["IsDlg"] != null)
      {
         context.Response.Write("<script      type=\"text/javascript\">window.frameElement.commonModalDialogClose(0, 'cancelled');</script>");
                    context.Response.Flush();
                    context.Response.End();
                    this.Page.Response.End();
                }
                else
                {
                    Response.Redirect(SPContext.Current.Web.Url);
                }
            }
            catch(Exception ex)
            {

                throw ex;
            }
        }

Programatically get User From Created By Field

SPFieldUser spUserField = (SPFieldUser)spListItem.Fields.GetField("Created By");
SPFieldUserValue spUserFieldValue = (SPFieldUserValue)spUserField.GetFieldValue(Convert.ToString(spListItem["Created By"]));