Saturday, July 17, 2010

Getting SharePoint User's Profiles


 SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                try
                {
                    using (SPSite oSite = new SPSite(SPContext.Current.Site.ID, SPContext.Current.Site.SystemAccount.UserToken))
                    {
                        SPServiceContext context = SPServiceContext.GetContext(oSite);
                        UserProfileManager myProfileManager = new UserProfileManager(context);
                        string CurrentUser = SPContext.Current.Web.CurrentUser.LoginName;
                        UserProfile myProfile = myProfileManager.GetUserProfile(CurrentUser);
                        dt.Rows.Add(Convert.ToString(myProfile["FirstName"].Value), Convert.ToString(myProfile["Department"].Value));
                        //dt.Rows.Add(Convert.ToString(myProfile["LastName"].Value), Convert.ToString(myProfile["FirstName"].Value), Convert.ToString(myProfile["Department"].Value), Convert.ToString(myProfile["WorkEmail"].Value), Convert.ToString(myProfile["WorkPhone"].Value), Convert.ToString(myProfile["CellPhone"].Value));

                    }
                    CurentUserGrd.DataSource = dt;
                    CurentUserGrd.DataBind();
                }
                catch (Exception ex)
                {
                    lblError.Text = ex.Message;
                }
            });

Wednesday, July 14, 2010

To Lock or Unlock a site collection by using Central Administration

TO LOCK A SITE COLLLECTION

1. Verify that you have the following administrative credentials. 
      • You must be a member of the Site Collection Administrators group for the site collection.

2. In Central Administration, click Application Management.

3. On the Application Management page, in the Site Collections section, click Configure quotas and locks. The Site Collection Quotas and Locks page opens.

4. If you want to change the selected site collection, in the Site Collection section, on the Site Collection menu, click Change Site Collection. Use the Select Site Collection page to select a site collection.

5. On the Site Collection Quotas and Locks page, in the Site Lock Information section, select one of the following options:
     •Not locked: To unlock the site collection and make it available to users.
     •Adding content prevented: To prevent users from adding new content to the 
       sitecollection. Updates and deletions are still allowed.
     •Read-only (blocks additions, updates, and deletions)
       To prevent users from adding, updating, or deleting content.
     •No access: To prevent access to content completely. 
       Users who attempt to access the site receive an access-denied message.

6. If you select Adding content prevented, Read-only (blocks additions, updates, and deletions), or No access, type a reason for the lock in the Additional lock information box.

7. Click OK.

TO UNLOCK A SITE COLLLECTION


1.Verify that you meet the following minimum requirements: See Add-SPShellAdmin.

2.On the Start menu, click All Programs.

3.Click Microsoft SharePoint 2010 Products.

4.Click SharePoint 2010 Management Shell.

5.At the Windows PowerShell 2.0 command prompt, type the following command:
      Set-SPSite -Identity "<SiteCollection>" -LockState "<State>"

Where:
•<SiteCollection> is the URL of the site collection that you want to lock or unlock.
•<State> is one of the following values:
Unlock: To unlock the site collection and make it available to users.
NoAdditions: To prevent users from adding new content to the site collection. Updates and deletions are still allowed.
ReadOnly: To prevent users from adding, updating, or deleting content.
NoAccess: To prevent access to content completely. Users who attempt to access the site receive an access-denied message.

Friday, February 12, 2010

How to consume Sharepoint Web Service through Javascript

Sharepoint 2010 has introduced Client Object Modal by which you can send a call to Sharepoint client API through code or through Javascript. But if you are calling the client API through Javascript, you can only do so by using asynchronous (executeQueryAsync) call to the native method.

Many a times you may need to get the data in the same method without leaving the control (means by a synchronous call without leaving the method). In that case, the best possible solution would be to use Web service calls.
Below is the code to accomplish this. For the sake of simplicity, I have called "Lists.asmx" web service to get an item by Id.

//Main Method
function GetItemByIdAndTitle(rootSiteUrl, ListTitle, itemId) {
var responseXML;
var ws;
try {
ws = InstanciateHttpRequest();
if (ws != null)
{
var soap = GetEnvelope(ListTitle, itemId); // create a web service envelope here
ws.open("POST", rootSiteUrl + "/_vti_bin/lists.asmx", false);
ws.setRequestHeader("Content-Type", "text/xml; charset=utf-8");
ws.setRequestHeader("SOAPAction", "http://schemas.microsoft.com/sharepoint/soap/GetListItems");
ws.send(soap);
if (ws.readyState == '4' && ws.status == '200')
{
responseXML = ws.responseXML.xml;
//you can parse your XML here to get the data
}
}
}
catch (e)
{ }
return responseXML;
}


//This ensures the the object instanciation is supported by multiple browsers // (Mozilla, IE).
function InstanciateHttpRequest()
{
var Obj;
if (window.XMLHttpRequest)
{
Obj = new XMLHttpRequest();
}
else
{
Obj = new ActiveXObject("Microsoft.XMLHTTP");
}
return Obj;
}

//Soap Envelope

function GetEnvelope(listName,itemId)
<
var soap = "<?xml version='1.0' encoding='utf-8'?>" +
"<soap:Envelope xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:soap='http://schemas.xmlsoap.org/soap/envelope/'>" +
"<soap:Body>" +
"<GetListItems xmlns='http://schemas.microsoft.com/sharepoint/soap/'>"+
"<listName>" + listName + "</listName>"+
"<query><Query><Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + itemId + "</Value></Eq></Where></Query></query>" +
"<viewFields><ViewFields><FieldRef Name='ContentType' /></ViewFields></viewFields>" +
"<rowLimit>1</rowLimit>" +
"<queryOptions><QueryOptions><ViewAttributes Scope='Recursive'></ViewAttributes></QueryOptions></queryOptions>" +
"</GetListItems>"+
"</soap:Body>" +
"</soap:Envelope>";
return soap;
>

 

Wednesday, February 10, 2010

How to add JQuery, JS and CSS file reference to sharepoint master page without modifying the master page

In SharePoint master page, there is  AdditionalPageHead Content Place Holder which can be used for adding JavaScript, Meta Tags, CSS Styles or other content to the page without modifying them.

One practical scenario would be programmatically adding JQuery, custom JS and CSS file reference to your master page without modifying the master page it.
You need to create a Delegate Control and deploy it using a feature. Below is the snapshot for Feature.XML file.
<?xml version="1.0" encoding="utf-8"?>
  <Control Id="AdditionalPageHead"
           ControlSrc="~/_controltemplates/CustomFolder/CustomPageHead.ascx"
           Sequence="1499"/>
</Elements>
Change the sequence number and control path and name as per your requirement. 

In the CustomPageHead.ascx add the references of JS/CSS
<script type="text/javascript" src="/_layouts/../JS/jquery-1.7.1.min.js"></script>
….
….
<link rel="stylesheet" type="text/css" href="/_layouts/../../../jquery-ui-1.8.18.custom.css" />
<script type="text/javascript">
</script>


Just deploy your feature and activate it in the site where you want to add JS and CSS references.

Tuesday, January 19, 2010

Step by step Sharepoint 2010 Modal Dialog Pop up


<script type="text/javascript">
     function OpenDialog(URL) {
         var NewPopUp = SP.UI.$create_DialogOptions();
         NewPopUp.url = URL;
         NewPopUp.width = 700;
         NewPopUp.height = 350;
         SP.UI.ModalDialog.showModalDialog(NewPopUp);
     }
 </script>
Call the Javascript on Button click and the modal PopUp will pops up with the Page of Specified URL.
btnOpenPopUp.Attributes.Add("onClick", "javascript:OpenDialog(‘/_layouts/CustomPage/CustomPage.aspx’);");

Sunday, January 10, 2010

How to show default sharepoint context menu with items in SharePoint 2010 custom webparts



While developing custom webparts, we often come across a requirement where in we need to show items from multiple lists in a grid like interface.While SPGridView is a nice control to achieve that,it has its own limitations when it comes to out of box features of sharepoint like showing default sharepoint context menu with options like 'View Properties' ,'Edit Properties' etc.


Usually , to create a context menu using SPGridView, we need to create a 'SPMenuField' in the grid and write our own code for each of the menu items.This approach is difficult to be able to achieve out of the box menu options like 'View Properties','Edit properties' etc.


The other option is to embed the javascript that sharepoint generates for context menu in the page's html. This way, this javascript would take care of rendering the context menu for us.This is the method that I have used to render this context menu.


In order to achieve this, I created a SPGridView with 'BoundField' instead of 'SPMenufield' in the user control (.ascx) of the visual webpart:


<SharePoint:SPGridView ID="gvMyDocuments" AutoGenerateColumns="false"
    runat="server" AllowSorting="true" onsorting="gvMyDocuments_Sorting"
    OnRowDataBound="gvMyDocuments_RowDataBound">
    <Columns>
    <asp:TemplateField>
    <ItemTemplate>
            <asp:Image runat="server" ID="imgDocIcon" ImageUrl="/_layouts/images/icgen.gif"/>
            <asp:Image runat="server" Visible="false" ID="imgOverlay" CssClass="ms-vb-icon-overlay" ImageUrl="/_layouts/images/icgen.gif"/>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:BoundField HeaderText ="Name" DataField="Title" HtmlEncode="false" HtmlEncodeFormatString="false" SortExpression="Title" HeaderStyle-HorizontalAlign="Left"/>
    <asp:BoundField HeaderText ="Modified" DataField="Modified" SortExpression="Modified" HeaderStyle-HorizontalAlign="Left"/>
    <asp:BoundField HeaderText ="ModifiedBy" DataField="ModifiedBy" SortExpression="ModifiedBy" HeaderStyle-HorizontalAlign="Left"/>
    <asp:BoundField HeaderText ="ID" DataField="ID" itemstyle-cssclass="hiddencol" HeaderStyle-CssClass="hiddencol" HeaderStyle-HorizontalAlign="Left"/>
    </Columns>
    <RowStyle HorizontalAlign="Left" VerticalAlign="Middle" />
</SharePoint:SPGridView>





While creating the entity collection to be bound with the grid view,I created the html and assigned it to the Title field (to be bound with the 'Name' field of the GridView).

The code for creation of this html is as follows:

objRelationships.Title = GenerateTitleHtml(strNameWoExt, objSPListItem.Url, itemCount, objSPListItem.ID, objSPListItem.ParentList, objSPListItem);


where:
 objSPListItem is the current SPListItem object being populated.
 strNameWoExt is the document name (without extention)
 objSPListItem.Url is the url for the current list item
 itemCount is a unique number being incremented everytime to maintain a unique id.
 objSPListItem.ID is the id of the list item.
 objSPListItem.ParentList is the parent list of the list item.



The method 'GenerateTitleHtml' creates all the html necessary for creating
the context menu :

public virtual String GenerateTitleHtml(String strTitle, String strFileUrl, Int32 itemCount, Int32 ItemID, SPList itemParentList, SPListItem currentListItem)
        {
            StringBuilder sbHtml = new StringBuilder();

            String ctxMenuJavascriptHtml = CreateContextMenu(itemParentList, itemCount);

            if (!String.IsNullOrEmpty(ctxMenuJavascriptHtml))
            {
                sbHtml.Append(ctxMenuJavascriptHtml);
            }

            String strCtxId = itemCount.ToString() + Math.Abs(itemParentList.ID.GetHashCode()).ToString().Substring(0, 3);

            sbHtml.Append("<div class='ms-vb itx' onmouseover='OnItem(this)' ctxname='ctx" + strCtxId + "' id='" + ItemID + "' Field='LinkFilename' Perm='0x7fffffffffffffff' EventType=''>");
            sbHtml.Append("<a onclick=\"" + GetDispEx(currentListItem, itemCount) + "\" href='/" + strFileUrl + "' onfocus='OnLink(this)'>" + strTitle + "<img height='1' border='0' width='1' alt='Use SHIFT+ENTER to open the menu (new window).' class='ms-hidden' src='/_layouts/images/blank.gif'></a>");
            // sbHtml.Append(strTitle);
            sbHtml.Append("</div>");
            sbHtml.Append("<div class='s4-ctx' onmouseover='OnChildItem(this.parentNode); return false;'>");
            sbHtml.Append("<span> </span>");
            sbHtml.Append("<a onfocus='OnChildItem(this.parentNode.parentNode); return false;' onclick='PopMenuFromChevron(event); return false;' href='javascript:;' title='Open Menu'></a>");
            sbHtml.Append("<span> </span>");
            sbHtml.Append("</div>");

            String customMenuActionsHtml = CreateCustomMenuActionsHtml(itemParentList, ItemID);

            if (!String.IsNullOrEmpty(customMenuActionsHtml))
            {
                sbHtml.Append(customMenuActionsHtml);
            }

            return sbHtml.ToString();
        }

        public String GetDispEx(SPListItem item, Int32 ctxID)
        {
            String strDispEx = String.Empty;

            String htmlFileType = String.Empty;

            String strCtxID = ctxID.ToString() + Math.Abs(item.ParentList.ID.GetHashCode()).ToString().Substring(0, 3);

            if (item["HTML_x0020_File_x0020_Type"] != null)
            {
                htmlFileType = item["HTML_x0020_File_x0020_Type"].ToString();
            }

            String strDefaultItemOpenApplication = (item.ParentList.DefaultItemOpen == DefaultItemOpen.Browser) ? "1" : "0";

            String strCurrentUserID = item.Web.CurrentUser.ID.ToString();
            String strFileName = "";
            if (item["FileRef"] != null)
            {
                strFileName = item["FileRef"].ToString();
            }
            String strRedirectToServerFile = SPEncode.ScriptEncode(SPUtility.MapToServerFileRedirect(item.ParentList.ParentWeb, strFileName, htmlFileType));

            if (!String.IsNullOrEmpty(strRedirectToServerFile))
            {
                strRedirectToServerFile = "1" + strRedirectToServerFile;
            }

            String strCheckedOutToLocal = "";

            if (item["IsCheckedoutToLocal"] != null)
            {
                strCheckedOutToLocal = item["IsCheckedoutToLocal"].ToString();
            }

            String strForceCheckout = item.ParentList.ForceCheckout ? "1" : "0";

            String strCheckedOutUserID = "";
            if (item["CheckedOutUserId"] != null)
            {
                strCheckedOutUserID = item["CheckedOutUserId"].ToString().Split(new char[] { '#' })[1];
            }

            String strPermMask = "";

            if (item["PermMask"] != null)
            {
                strPermMask = item["PermMask"].ToString();
            }

            String strSetContext = "ctx=g_ctxDict['ctx" + strCtxID + "'];";

            strDispEx = String.Format("return DispEx(this,event,'TRUE','FALSE','FALSE','SharePoint.OpenDocuments.3','{0}','SharePoint.OpenDocuments','{1}','{2}','{3}','{4}','{5}','{6}','{7}')", new object[] { strDefaultItemOpenApplication, htmlFileType, strRedirectToServerFile, strCheckedOutUserID, strCurrentUserID, strForceCheckout, strCheckedOutToLocal, strPermMask });

            return strSetContext + strDispEx;
        }

        public virtual String CreateCustomMenuActionsHtml(SPList itemParentList, Int32 ItemID)
        {
            StringBuilder customMenuActionsHtml = new StringBuilder();

            if (!customMenuActionsDivListIDs.Contains(itemParentList.ID.ToString()))
            {
                customMenuActionsHtml.AppendLine("<div id='ECBItems_{" + itemParentList.ID.ToString() + "}' style='display:none' height='0' width='0'>");

                foreach (SPUserCustomAction objSPUserCustomAction in itemParentList.ParentWeb.UserCustomActions)
                {

                    customMenuActionsHtml.AppendLine("<div>");
                    customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.Title + "</div>");
                    customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.ImageUrl + "</div>");
                    customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.Url + "</div>");
                    customMenuActionsHtml.AppendLine("<div>0x0</div>");
                    if (objSPUserCustomAction.Rights == SPBasePermissions.EditListItems)
                    {
                        customMenuActionsHtml.AppendLine("<div>0x4</div>");
                    }
                    else
                    {
                        customMenuActionsHtml.AppendLine("<div>0x0</div>");
                    }

                    customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.RegistrationType.ToString() + "</div>");

                    if (objSPUserCustomAction.RegistrationId != null)
                    {
                        customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.RegistrationId + "</div>");
                    }

                    customMenuActionsHtml.AppendLine("<div>" + objSPUserCustomAction.Sequence.ToString() + "</div>");

                    customMenuActionsHtml.AppendLine("</div>");


                }

                customMenuActionsHtml.AppendLine("</div>");

                customMenuActionsDivListIDs.Add(itemParentList.ID.ToString());
            }

            return customMenuActionsHtml.ToString();
        }

        private String CreateContextMenu(SPList objSPList, Int32 ctxID)
        {
            StringBuilder javascript = new StringBuilder();

            #region variables

            String strListItemTitle = String.Empty;
            Int32 listBaseType = 0;
            Boolean navigateForFormsPages = false;
            String listTemplateID = String.Empty;
            String listName = String.Empty;
            String viewID = String.Empty;
            String strListUrlID = String.Empty;
            String strHttpPath = String.Empty;
            String strHttpRoot = String.Empty;
            String strImagesPath = String.Empty;
            String strPortalUrl = String.Empty;
            String strSendToLocationName = String.Empty;
            String strSendToLocationUrl = String.Empty;
            Int32 recycleBinEnabled = 0;
            String strOfficialFileName = String.Empty;
            String strOfficialFileNames = String.Empty;
            String strWriteSecurity = String.Empty;
            String strSiteTitle = String.Empty;
            String strListTitle = String.Empty;
            String strDisplayFormUrl = String.Empty;
            String strEditFormUrl = String.Empty;
            Int32 isWebEditorPreview = 0;
            //Int32 ctxId = 0;
            String ctxId = String.Empty;
            Boolean isXslView = false;
            SPUser currentUser;
            Boolean enableMinorVersions = false;
            Int32 verEnabled = 0;
            Boolean workflowsAssociated = false;
            Boolean contentTypesEnabled = false;
            String strCtrlName = String.Empty;
            Int32 relatedCascadeLists = 0;
            String strSource = String.Empty;
            String strMenuHome = String.Empty;
            String strHomeURL = String.Empty;
            String strHomeImageURL = String.Empty;
            String strMenuMyDoc = String.Empty;
            String strMyDocURL = String.Empty;
            String strMyDocImageURL = String.Empty;
            String strMenuAudit = String.Empty;
            String strAuditURL = String.Empty;
            String strAuditImageURL = String.Empty;
            String strMenuRollout = String.Empty;
            String strRolloutAction = String.Empty;
            String strMenuVersionPIM = String.Empty;
            String strVersionPIMAction = String.Empty;
            String strMenuCompliance = String.Empty;
            String strComplianceAction = String.Empty;
            Boolean isRolloutHASub = false;
            Boolean isVersionPIM = false;
            Int32 itemID = 0;

            #endregion

            #region Current Context Variables

            listBaseType = (Int32)Enum.Parse(typeof(SPBaseType), objSPList.BaseType.ToString());
            navigateForFormsPages = objSPList.NavigateForFormsPages == true ? true : false;
            listTemplateID = ((Int32)objSPList.BaseTemplate).ToString();
            listName = "{" + objSPList.ID.ToString().ToUpper() + "}";
            viewID = "{" + objSPList.DefaultView.ID.ToString().ToUpper() + "}";
            //strListUrlID = "/" + Page.Server.UrlEncode(objSPList.RootFolder.Url.ToString());
            strListUrlID = "/" + SPHttpUtility.UrlPathEncode(objSPList.RootFolder.Url.ToString(), true);
            strHttpPath = SPContext.Current.Site.Url + "/_vti_bin/owssvr.dll?CS=65001";
            strHttpRoot = SPContext.Current.Site.Url;
            strImagesPath = "/_layouts/images/";
            strPortalUrl = SPContext.Current.Web.PortalUrl == String.Empty ? null : SPContext.Current.Web.PortalUrl;
            strSendToLocationName = objSPList.SendToLocationName;
            strSendToLocationUrl = objSPList.SendToLocationUrl;
            recycleBinEnabled = SPContext.Current.Web.Site.WebApplication.RecycleBinEnabled == true ? 1 : 0;
            strOfficialFileName = SPContext.Current.Web.Site.WebApplication.OfficialFileName;
            strOfficialFileNames = "Relationships";//added for the sake of identification in js file
            strWriteSecurity = Convert.ToString(objSPList.WriteSecurity);
            strSiteTitle = SPContext.Current.Web.Title;

            strListTitle = objSPList.Title;
            strDisplayFormUrl = SPContext.Current.Site.Url + "/_layouts/listform.aspx?PageType=4&ListId={" + (Convert.ToString(objSPList.ID).ToUpper() + "}");//objSPList.Forms[PAGETYPE.PAGE_DISPLAYFORM].Url;
            strEditFormUrl = SPContext.Current.Site.Url + "/_layouts/listform.aspx?PageType=6&ListId={" + (Convert.ToString(objSPList.ID).ToUpper() + "}");//objSPList.Forms[PAGETYPE.PAGE_EDITFORM].Url;
            isWebEditorPreview = 0;
            //ctxId = objSPList.ItemCount;
            ctxId = ctxID.ToString() + Math.Abs(objSPList.ID.GetHashCode()).ToString().Substring(0,3);
            isXslView = false;
            currentUser = SPContext.Current.Web.CurrentUser;
            enableMinorVersions = objSPList.EnableMinorVersions;
            verEnabled = objSPList.EnableVersioning == true ? 1 : 0;
            workflowsAssociated = objSPList.WorkflowAssociations.Count > 0 ? true : false;
            contentTypesEnabled = objSPList.ContentTypesEnabled;
            relatedCascadeLists = 1;
            #endregion

            #region Constructing javascript for context object

            javascript.AppendLine("<script type='text/javascript'>");
            //javascript.Append("debugger;");
            javascript.AppendLine("ctx = new ContextInfo();");
            javascript.AppendLine("var existingHash = '';");
            javascript.AppendLine("if (window.location.href.indexOf('#') > -1)");
            javascript.AppendLine("{");
            javascript.AppendLine("existingHash = window.location.href.substr(window.location.href.indexOf('#'));");
            javascript.AppendLine("}");
            javascript.AppendLine("ctx.existingServerFilterHash = existingHash;");
            javascript.AppendLine("if (ctx.existingServerFilterHash.indexOf('ServerFilter=') == 1) {");

            javascript.AppendLine("ctx.existingServerFilterHash = ctx.existingServerFilterHash.replace(/-/g, '&').replace(/&&/g, '-');");
            javascript.AppendLine("var serverFilterRootFolder = GetUrlKeyValue('RootFolder', true, ctx.existingServerFilterHash);");
            javascript.AppendLine("var currentRootFolder = GetUrlKeyValue('RootFolder', true);");
            javascript.AppendLine("if ('' == serverFilterRootFolder && '' != currentRootFolder) {");
            javascript.AppendLine("ctx.existingServerFilterHash += '&RootFolder=' + currentRootFolder;");
            javascript.AppendLine("}");
            javascript.AppendLine("window.location.hash = '';");
            javascript.AppendLine("window.location.search = '?' + ctx.existingServerFilterHash.substr('ServerFilter='.length + 1);");
            javascript.AppendLine("}");


            javascript.AppendLine("ctx.listBaseType = " + SPHttpUtility.NoEncode(listBaseType == null ? -1 : listBaseType) + ";");

            javascript.AppendLine("var varNavigateForFormsPages='" + navigateForFormsPages + "';");

            javascript.AppendLine("if(varNavigateForFormsPages=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.NavigateForFormsPages =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.NavigateForFormsPages =  true;");
            javascript.AppendLine("}");

            javascript.AppendLine("ctx.listTemplate = '" + listTemplateID + "';");
            javascript.AppendLine("ctx.listName = '" + listName + "';");
            javascript.AppendLine("ctx.view = '" + viewID + "';");
            javascript.AppendLine("ctx.listUrlDir = '" + strListUrlID + "';");
            javascript.AppendLine("ctx.HttpPath = '" + strHttpPath + "';");
            javascript.AppendLine("ctx.HttpRoot = '" + strHttpRoot + "';");
            javascript.AppendLine("ctx.imagesPath = '" + strImagesPath + "';");
            javascript.AppendLine("ctx.PortalUrl = '" + strPortalUrl + "';");
            javascript.AppendLine("ctx.SendToLocationName = '" + strSendToLocationName + "';");
            javascript.AppendLine("ctx.SendToLocationUrl =  '" + strSendToLocationUrl + "';");
            javascript.AppendLine("ctx.RecycleBinEnabled = " + recycleBinEnabled + ";");
            javascript.AppendLine("ctx.OfficialFileName =  '" + strOfficialFileName + "';");
            javascript.AppendLine("ctx.OfficialFileNames = '" + strOfficialFileNames + "';");
            javascript.AppendLine("ctx.WriteSecurity =  '" + strWriteSecurity + "';");
            javascript.AppendLine("ctx.SiteTitle =  '" + strSiteTitle + "';");
            javascript.AppendLine("ctx.ListTitle =   '" + strListTitle + "';");


            javascript.AppendLine("var varIsForceCheckout= '" + objSPList.ForceCheckout + "';");
            javascript.AppendLine("if(varIsForceCheckout=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.isForceCheckout =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.isForceCheckout = true;");
            javascript.AppendLine("}");


            javascript.AppendLine("if (ctx.PortalUrl == '')");
            javascript.AppendLine("ctx.PortalUrl = null;");
            javascript.AppendLine("ctx.displayFormUrl =  '" + strDisplayFormUrl + "';");
            javascript.AppendLine("ctx.editFormUrl = '" + strEditFormUrl + "';");
            javascript.AppendLine("ctx.isWebEditorPreview = " + isWebEditorPreview + ";");
            javascript.AppendLine("ctx.ctxId = " + SPHttpUtility.NoEncode(ctxId == null ? "-1" : ctxId.ToString()) + ";");

            javascript.AppendLine("var varIsXslView= '" + isXslView + "';");
            javascript.AppendLine("if(varIsXslView=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.isXslView =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.isXslView = true;");
            javascript.AppendLine("}");

            javascript.AppendLine("if (g_ViewIdToViewCounterMap['" + viewID + "'] == null)");
            javascript.AppendLine("g_ViewIdToViewCounterMap['" + viewID + "'] = " + SPHttpUtility.NoEncode(ctxId == null ? "-1" : ctxId.ToString()) + ";");

            javascript.AppendLine("ctx.CurrentUserId = " + SPHttpUtility.NoEncode(currentUser == null ? "-1" : currentUser.ID.ToString()) + ";");

            javascript.AppendLine("var varEnableMinorVersions= '" + enableMinorVersions + "';");
            javascript.AppendLine("if(varEnableMinorVersions=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.EnableMinorVersions =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.EnableMinorVersions = true;");
            javascript.AppendLine("}");

            javascript.AppendLine("ctx.verEnabled = " + verEnabled + ";");

            javascript.AppendLine("var varWorkflowsAssociated='" + workflowsAssociated + "';");

            javascript.AppendLine("if(varWorkflowsAssociated=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.WorkflowsAssociated =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.WorkflowsAssociated = true;");
            javascript.AppendLine("}");

            javascript.AppendLine("var varContentTypesEnabled='" + contentTypesEnabled + "';");
            javascript.AppendLine("if(varContentTypesEnabled=='False')");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.ContentTypesEnabled =  false;");
            javascript.AppendLine("}");
            javascript.AppendLine("else");
            javascript.AppendLine("{");
            javascript.AppendLine("ctx.ContentTypesEnabled = true;");
            javascript.AppendLine("}");
            javascript.AppendLine("ctx.HasRelatedCascadeLists = " + relatedCascadeLists + ";");

            javascript.AppendLine("ctx" + ctxId + " = ctx;");
            javascript.AppendLine("g_ctxDict['ctx" + ctxId + "'] = ctx;");

            javascript.AppendLine("</script>");

            #endregion

            return javascript.ToString();
        }

Monday, January 5, 2009

Deploying Web Parts with Solution Packages in MOSS 2007

Back in the good ‘ol days of WSS v2 and SPS 2003, we deployed web parts in CAB files called wppacks.  While wppacks can still be used to distrubute web parts in WSS v3 and MOSS 2007, a more compelling method is now to deploy your custom web parts along with features, site definitions and other artifacts in a SharePoint solution package.
The manifest.xml for a SharePoint solution package that includes web parts might look something like this:
<?xml version="1.0" encoding="utf-8" ?>
<Solution SolutionId="1F693B5D-87CB-4509-83E7-8681E7B032A3"
    xmlns="http://schemas.microsoft.com/sharepoint/">
 
  <FeatureManifests>
    <FeatureManifest Location="SPSolutionsDemoFeature\Feature.xml"/>
  </FeatureManifests>
  <SiteDefinitionManifests>
    <SiteDefinitionManifest Location="SPSolutionsDemo">
      <WebTempFile Location="1033\xml\webtempspsolutionsdemo.xml"/>
    </SiteDefinitionManifest>
  </SiteDefinitionManifests>
 
  <TemplateFiles>
    <TemplateFile Location="ControlTemplates\SPSolutions\SPSolutions.Demo.MyUserControlTemplate.ascx"/>
    <TemplateFile Location="ControlTemplates\SPSolutions\SPSolutions.Demo.SimpleControlTemplate.ascx"/>
    <TemplateFile Location="Layouts\ClientQueryTest.aspx"/>
  </TemplateFiles>
 
  <Assemblies>
    <Assembly DeploymentTarget="WebApplication"
          Location="SPSolutions.Demo.WebParts.dll">
      <SafeControls>
        <SafeControl Assembly="SPSolutions.Demo.WebParts, Version=1.0.0.0, Culture=neutral, PublicKeyToken=e9db3057acd9c0f6"
          Namespace="SPSolutions.Demo.WebParts"
          TypeName="*" Safe="True" />
      </SafeControls>
    </Assembly>
  </Assemblies>
 
</Solution> 
Note the <SafeControl> element within <Assemblies><Assembly>.  This same <SafeControl> element will be reflected within SharePoint’s web.config upon successful installation of the solution package. 
The deployable CAB file is built using a tried-and-true utility from Microsoft called makecab.exe.  Makecab has been around since most those smart, young kids at Microsoft still had running noses and can be downloaded as part of the Microsoft Cabinet Software Development Kit.  The command to create a CAB file takes a configuration file or “DDF” as a parameter and looks something like this:
makecab.exe /f package.ddfThe contents of the DDF file tell makecab how to contruct the CAB file.  An  example DDF file might look something like this:
;*** Sample Source Code MakeCAB Directive file example
;
.OPTION EXPLICIT ; Generate errors
.Set CabinetNameTemplate=SPSolutionsDemoPackage.cab   
.set DiskDirectoryTemplate=CDROM ; All cabinets go in a single directory
.Set CompressionType=MSZIP ;** All files are compressed in cabinet files
.Set UniqueFiles="OFF"
.Set Cabinet=on
.Set DiskDirectory1=Package
manifest.xml
Assemblies\SPSolutions.Demo.WebParts.dll
.Set DestinationDir=ControlTemplates\SPSolutions
ControlTemplates\SPSolutions\SPSolutions.Demo.MyUserControlTemplate.ascx
ControlTemplates\SPSolutions\SPSolutions.Demo.SimpleControlTemplate.ascx
.Set DestinationDir=Layouts
Layouts\ClientQueryTest.aspx
.Set DestinationDir=SPSolutionsDemoFeature
Features\SPSolutionsDemoFeature\Feature.xml
Features\SPSolutionsDemoFeature\Elements.xml
.Set DestinationDir=SPSolutionsDemo
SiteTemplates\SPSolutionsDemo\default.aspx
SiteTemplates\SPSolutionsDemo\defaultdws.aspx
SiteTemplates\SPSolutionsDemo\ONET.xml
.Set DestinationDir=1033\xml
1033\xml\webtempspsolutionsdemo.XML
;*** <the end>
Once the redistributable cab file is created, we can use the stsadm.exe tool (located in c:\program files\common files\microsoft shared\web server extensions\12\bin\) that comes with SharePoint to actually  deploy the solution package.  Deploying a solution package is a two-step process that first involves adding the package to SharePoint’s solution store like this:
stsadm -o addsolution -filename SPSolutionsDemoPackage.cabAnd then deploying the solution to a particular scope like this:
stsadm -o deploysolution -name SPSolutionsDemoPackage.cab -local -url "siteurl"