18.1.12

Why it returns null (control)TemplateContainer.FindControl("controlId")?

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register TagPrefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Register TagPrefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages"
    Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="LookUpProtocolCodeFieldControl.ascx.cs"
    Inherits="LookUpProtocolCodeProject.CONTROLTEMPLATES.LookUpProtocolCodeFieldControl"
%>
<SharePoint:RenderingTemplate runat="server" ID="LookUpProtocolCodeFieldControlTemplate"  >
    <Template>
        <table>
            <tr>
                <td>
                    <asp:DropDownList runat="server" ID="DDLLookUpProtocolCodeId">
                    </asp:DropDownList>
                </td>
            </tr>
        </table>
    </Template>
</SharePoint:RenderingTemplate>


protected override void CreateChildControls()
        {
            try
            {
                if (Field == null && this.ControlMode == SPControlMode.Display)
                { return; }

                base.CreateChildControls();

                DDLLookUpProtocolCode = (DropDownList)TemplateContainer.FindControl("DDLLookUpProtocolCodeId");
                if (DDLLookUpProtocolCode == null)
                    throw new SPException("Error: Cannot load the controls!");

                this.DDLLookUpProtocolCode.SelectedIndexChanged += new EventHandler(DropDownList_SelectedIndexChanged);
          

                DDLLookUpProtocolCode.TabIndex = TabIndex;
                DDLLookUpProtocolCode.CssClass = CssClass;
                DDLLookUpProtocolCode.ToolTip = Field.Title;
                PopulateDropDown();
            }
            catch (Exception ex)
            {
        ..
            }
        }

17.1.12

Writing to the SharePoint ULS Logs

try{
  ...
} catch (Exception ex) {
  SPDiagnosticsService.Local.WriteTrace(0, new SPDiagnosticsCategory("MSDN", TraceSeverity.Unexpected, EventSeverity.Error), TraceSeverity.Unexpected, ex.Message, ex.StackTrace);
}

 

Product column is Unknown. This is the default behavior of the SPDiagnosticsService class

Unfortunately, interacting with a custom SPDiagnosticsService is available only in fully trusted farm solutions and not within sandboxed solutions.

To write to the ULS logs by using theSPDiagnosticsService class from the sandbox, developers can create a full trust proxy that the sandboxed solutions can call into

SPDeveloperDashboardLevel Dashboard SharePoint Display

$contentService = [Microsoft.SharePoint.Administration.SPWebService]::ContentService
$dashboardSetting = $contentService.DeveloperDashboardSettings
$dashboardSetting.DisplayLevel = [Microsoft.SharePoint.Administration.SPDeveloperDashboardLevel]::On
$dashboardSetting.Update()

EventFiringEnabled vs EventFiringEnabled

 

 

 

private static void EventFiringEnabled(bool enabled)

{

Assembly assembly = Assembly.Load("Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c");

Type type = assembly.GetType("Microsoft.SharePoint.SPEventManager");

PropertyInfo pi = type.GetProperty("EventFiringDisabled", System.Reflection.BindingFlags.Static|System.Reflection.BindingFlags.NonPublic);

pi.SetValue(null, !enabled, null);

}

SharePoint and Linq : speed code in visual web part

proxy generation


//in solution folder

set path=%path%;c:\program files\common files\microsoft shared\web server extensions\14\bin

spmetal.exe /web:{url sharepoint site} /namespace:{solution namespace} /code:{codeClassName}.cs


.aspx


<%@ Import Namespace="Microsoft.SharePoint.WebControls" %>
<SharePoint:SPGridView id="spGridView" runat="server" AutoGenerateColumns="false">
  <HeaderStyle HorizontalAlign="Left" ForeColor="Navy" Font-Bold="true" />
  <Columns>
    <SharePoint:SPBoundField  DataField="Title" HeaderText="Title"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="JobTitle" HeaderText="JobTitle"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="ProjectTitle" HeaderText="ProjectTitle"></SharePoint:SPBoundField>
    <SharePoint:SPBoundField DataField="DueDate" HeaderText="DueDate"></SharePoint:SPBoundField>
  </Columns>
</SharePoint:SPGridView>


.cs


using Microsoft.SharePoint.Linq;
using Microsoft.SharePoint;
using System.Linq;

{codeClassName} dc = new {codeClassName}(SPContext.Current.Web.Url); 
EntityList<EmployeesItem> Employees = dc.GetList<EmployeesItem>("Employees"); 
var empQuery = from emp in Employees
               where emp.Project.DueDate < DateTime.Now.AddMonths(6)
               select new
               {
                   emp.Title,
                   emp.JobTitle,
                   ProjectTitle = emp.Project.Title,
                   DueDate = emp.Project.DueDate.Value.ToShortDateString()
               }; 
spGridView.DataSource = empQuery;
spGridView.DataBind();

Web Part property attributes

Browsable
Set to false if you don't want to display the custom property in the property pane. Also, if you set the WebPartStorage attribute to Storage.None, your property won't display in the property pane.

Category
The title of the section of the property pane that you want created to display the custom property. If you don't specify the Category attribute or if you specify the Categoryattribute as "Default", your custom property is displayed in the Miscellaneous section of the property pane.

DefaultValue
The default value of the custom property. Specifying the default value minimizes the Web Part's storage requirements by storing the property's value only if it is different from the default.

Description
The contents of the tool tip that appears when you pause the mouse pointer over the custom property in the property pane.

FriendlyNameAttribute
The caption displayed for the custom property in the property pane. If you don't specify this attribute, the actual property name will be displayed in the property pane.

HtmlDesignerAttribute
Used to associate a property builder with the property.

ReadOnly
Set to true if you want the custom property to be read-only in the property pane.

ResourcesAttribute
Used to provide localized names for FriendlyName, Category, and Description in the tool pane for the custom property.

WebPartStorage
Set to Storage.Shared to display the custom property in the property pane when the user is in shared view of the page. Set to Storage.Personal to display the custom property in the property pane when the user is in Shared or Personal view of the page. Set to Storage.None if you don't want the setting to persist for the custom property. The custom property won't be displayed in the property pane.

 

[Category("Custom Properties")]
[DefaultValue(c_MyBoolDefault)]
[WebPartStorage(Storage.Personal)]
[FriendlyNameAttribute("Custom Boolean")]
[Description("Select to set value to True.")]
[Browsable(true)]
[XmlElement(ElementName="MyBoolean")]
// The accessor for this property.
public bool MyBool
{
get
{
return _myBool;
}
set
{
_myBool = value;
}
}

Hidden list SharePoint WebPart

 

Vous vous cacher ou montrer vos listes

 

image

 

Download WebPart

16.1.12

SharePoint and COM and Jquery - example

 

  1. Put JQuery distribution in 14hive\Layouts.
  2. Add Javascript link referral in the AdditionalPageHead place holder:

    <SharePoint:ScriptLink Language="Javascript" Localizable="False" runat="server" Name="jquery-1.4.4.min.js"/>
    <SharePoint:ScriptLink Name="SP.js" runat="server" OnDemand="true" Localizable="false"/>

  3. Add Javascript code and create Client Object Model call to retrieve value in Main conten place holder:

 

<script type="text/javascript"> 
// JQuery_Script

var spListItems;

function FilterLookup(filterSource, lookupList)
{

//select lookupcontrol
var filterElement = $("select[title='" + filterSource + "'] option:selected");
if(filterElement.length == 0) return;

var selectedFilterText = filterElement.text();
var selectedFilterValue = filterElement.value;

var clientCtx = new SP.ClientContext.get_current();
var spList = clientCtx.get_web().get_lists().getByTitle(lookupList);

var camlQuery = new SP.CamlQuery();
camlQuery.set_viewXml('<View><Query><Where><Eq><FieldRef Name=\''+ filterSource +'\'/>' +
'<Value Type=\'Text\'>'+ selectedFilterText +'</Value></Eq></Where></Query></View>');

this.spListItems = spList.getItems(camlQuery);
clientCtx.load(spListItems);
clientCtx.executeQueryAsync(Function.createDelegate(this, this.onQuerySucceeded), Function.createDelegate(this, this.onQueryFailed) );

} //

function onQuerySucceeded(sender, args) {

alert(this.selectedFilterText);
// Get CityElement Select or Input
var cityElement = $("select[title='City']");
if(cityElement.length == 0) { alert('Unknown'); }

var options = '';
var listItemEnumerator = spListItems.getEnumerator();

while (listItemEnumerator.moveNext()) {
var oListItem = listItemEnumerator.get_current();
options += '<option value="' + oListItem.get_id() + '">' + oListItem.get_item('Title') +'</option>';
}
cityElement.html(options);
}

function onQueryFailed(sender, args) {
alert('Request failed. ' + args.get_message() + '\n' + args.get_stackTrace());
}


// Attach function on document ready
$(document).ready(
function() {
ExecuteOrDelayUntilScriptLoaded(FilterLookup,"sp.js");
//FilterLookup("Country","City");

// Get CountryElement Select or Input
var countryElement = $("select[title='Country']");
if(countryElement.length == 0)
{
countryElement = $("input[title='Country']");
}
if(countryElement.length != 0) {
if(countryElement[0].optHid) {
$("input[id='" + countryElement[0].optHid + "']").bind("propertychange", function() { FilterCity(); });

} else{
$("select[title='Country']").change(function() { FilterLookup("Country","City"); });

}

}


}); // Document ready

</script>



 





NOTE: example and not complete code

12.1.12

No link to list item lookup

 

  1. sharepoint design 2010
  2. select field
  3. set disable-output-escaping = no link item lookup
  4. set value list item to


<xsl:value-of select="substring-before(substring-after(string($thisNode/@*[name()=current()/@DisplayName]),'&gt;'),'&lt;')" disable-output-escaping="no"></xsl:value-of>

Block Application Pages

All the pages having _layouts before them are application pages. Pages created automatically for various views are called Form Pages. Most often with SharePoint implementation we allow users to access these pages. However we may further want to cut down access of users from the application pages and the form pages.

SharePoint allows this by enabling the feature “ViewFormPagesLockDown”. This feature is activated at the Site Collection scope. All groups / users not having the “View Application Pages” permission will not be able to navigate to pages like “_layouts/viewlsts.aspx” or “pages/forms/allitems.aspx”.
Below are the steps to block access from application pages:

  1. Identify users / group to restrict.
  2. Set their permission to "Restricted Read" or remove the "View Application Pages" from existing assigned permission level.
  3. Enable "ViewFormPagesLockDown" feature using the command - stsadm -o activatefeature -url -filename ViewFormPagesLockDown\feature.xml

The above steps will block all users not having "View Application Pages" permission from accessing the application pages and form pages.