Search This Blog

Wednesday, 30 May 2012

Hide/Show SPRibbon Based on User PermissionString in SPD


  Here i am going to explain to you, how to hide/Show ribbopn controls based on User permission in SharePoint Site.

  1. Just call the Jquery file in master page header section look like below.

  <script type="text/javascript"
  src="/Style%20Library/jquery-1.6.1.min.js"></script>

  2. We need to be remove the welcome menu control from
  SharePoint:DelegateControl, which was inside of this controls.

     
  3. Add this below DIV controls after SPRibbon DIV tag closed.

  <div class="s4-trc-container-menu1" style="text-align:right;float:right;width:100%; position:absolute;margin-top:10px;">
 <div id="notificationArea1" class="s4-noti" style="right: 16px;
  top: 0px; height: 22px">
<wssuc:Welcome id="IdWelcome1" runat="server"
EnableViewState="false"></wssuc:Welcome>
<wssuc:MUISelector ID="IdMuiSelector1" runat="server"/>
</div>
</div>

  4. Then finally Add below script before body tag closed.

        <script type="text/javascript">
                var UserHasPermissions=false;
                </script>

   <Sharepoint:SPSecurityTrimmedControl runat="server" PermissionsString="ManageWeb">
   <script type="text/javascript">
     UserHasPermissions=true;
  </script>
  </SharePoint:SPSecurityTrimmedControl>

 <script type="text/javascript">
if(UserHasPermissions)
                {
                document.getElementById("s4-trc-container-menu1").style.display="none";
                }
                else
                {
                document.write('<scr'+'ipt type="text/javascript" src="/Style Library/RemoveWelcomeMenuOption.js"></scr'+'ipt>');

                                if(getQueryVariable("IsDlg") != "undefined")
                                if(getQueryVariable("IsDlg")=="1")
                                {
                                document.getElementById("s4-ribboncont").style.display ="inline";
                                }
                                else
                                {
                                document.getElementById("s4-ribboncont").style.display ="none";
                                document.getElementById("s4-trc-container-menu1").style.display="block";
                                }

                function getQueryVariable(varname) {
                var query = window.location.search.substring(1);
                var vars = query.split("&");
                for (var i=0;i<vars.length;i++)
                {
                var pair = vars[i].split("=");
                if (pair[0] == varname)
                {
                return pair[1];
                }
                }
                }

                }
                </script>


   RemoveWelcomeMenuOption.js

jQuery(document).ready(function($){
  $("[text='My Settings']").remove();
  $("[text='Personalize this Page']").remove();
});
Please download this .JS file :jquery-1.6.1.min.js

InfoPath 2010: Repeating table incremental row number

Just add the below content in the properties of defaulf value:
count(../preceding-sibling::*/my:txtSNo) + 1

Friday, 6 April 2012

Export SharePoint List items to Word Document with Rich Text Box

Export SharePoint List items to Word Document


C# Code:


protected void btnExportToWord_Click(object sender, EventArgs e)
{
try
{
this.varExport = true;
}
catch (Exception ex)
{
throw ex;
}
}
protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();

}
base.Render(writer);
}

protected override void Render(HtmlTextWriter writer)
{
if (varExport)
{
Exportword();

}
base.Render(writer);

}
private void Exportword()
{
try
{
using (SPSite osite = new SPSite(SPContext.Current.Site.ID))
{
using (SPWeb oweb = osite.OpenWeb())
{
string siteUrl = osite.Url;
//Creating HttpContext
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentEncoding = System.Text.Encoding.Unicode;
HttpContext.Current.Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());
//HttpContext.Current.Response.Charset = "UTF-8";
HttpContext.Current.Response.ContentType = "application/vnd.ms-word";
//File name for the exported word document
string strFileName = "SampleDocument" + ".doc";
HttpContext.Current.Response.AddHeader("Content-Disposition", "inline;filename=" + strFileName);
//Giving heading to the Word Document with style
StringBuilder strHTMLMetaContent = new StringBuilder();
//Setting the Header content and Footer content
strHTMLMetaContent = GettingHeaderFooter();
strHTMLMetaContent.Append("EXPORT TO WORD SAMPLE".ToString());
//Getting the Job Aid metadata details
strHTMLMetaContent.Append(GettingJobAidMetadata(oweb));
//Getting the Job Aid Section details
strHTMLMetaContent.Append(GettingJobAidSectiondata(oweb));
strHTMLMetaContent.Append("


".ToString());
HttpContext.Current.Response.Write(strHTMLMetaContent);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
}
}
catch (Exception ex)
{
throw ex;
}
}
private StringBuilder GettingJobAidMetadata(SPWeb oweb)
{
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Documents");
SPQuery oquery = new SPQuery();
oquery.Query = "52";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oitem in oitemcoll)
{
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
strHTMLContent.Append("
".ToString());
}
strHTMLContent.Append("Job Aid No: "+Convert.ToString(oitem["JobAidNo"])+ ".ToString())"; 
}

}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;

}

private StringBuilder GettingJobAidSectiondata(SPWeb oweb)
{
string siteUrl = oweb.Site.Url;
StringBuilder strHTMLContent = new StringBuilder();
try
{
SPList olist = oweb.Lists.TryGetList("Job Aid Sections");
SPQuery oquery = new SPQuery();
oquery.Query = "520";
SPListItemCollection oitemcoll = olist.GetItems(oquery);
if (oitemcoll.Count > 0)
{
strHTMLContent.Append("".ToString());
foreach (SPListItem oListItem in oitemcoll)
{
SectionContent = Convert.ToString(oListItem["SectionContent"]).Replace("src=\"/", "src=\"" + siteUrl + "/");
//SectionContent = Regex.Replace(SectionContent, "<(p)>.\\s?<(.?p)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "<(span.*?)> <(.?span)>", string.Empty);
//SectionContent = Regex.Replace(SectionContent, "]+>.\\s?", string.Empty);
strHTMLContent.Append("
");
strHTMLContent.Append("
");
}
strHTMLContent.Append(" + oListItem["Title"].ToString() + " + SectionContent +".ToString());
}
}
catch (Exception ex)
{
throw ex;
}
return strHTMLContent;
}

Wednesday, 8 February 2012

Programmatically: How to Convert the username to SPUser in Sharepoint 2010

static SPUser GetSPUserObject(SPListItem spListItem, String fieldName)
{
SPUser spUser = null;
try
{
if (fieldName != string.Empty)
{
SPFieldUser field = spListItem.Fields[fieldName] as SPFieldUser;
if (field != null && spListItem[fieldName] != null)
{
SPFieldUserValue fieldValue = field.GetFieldValue(spListItem[fieldName].ToString()) as SPFieldUserValue;
if (fieldValue != null)
{
spUser = fieldValue.User;
}
}
}
}
catch (Exception ex)
{
//objError.SetErrorLog("CreateActionItemUserControl.GetSPUserObject", ex.ToString());
}
return spUser;
}