Format Date and Time

July 12, 2012 3 comments

Format date and time :

m_CalculatedOutOfOfficeStartDate = outOfOfficeStartDate.ToString("dd/MM/yyyy");
m_CalculatedOutOfOfficeEndDate = outOfOfficeEndDate.ToString("dd/MM/yyyy");

Hope this helps-
Rehman.

Ensure trailing slash

Sometimes you don’t know if you’ll get a trailing slash or not with a web.Url or site.Url. Use the method:

public static string EnsureTrailingSlash(string strUrl)
        {
            if (!strUrl.EndsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                strUrl += "/";
            }
            return strUrl;
        }

Call like this:
SharedUrlUtility.EnsureTrailingSlash(SharedUserProfiles.MySiteHostUrl);

Hope this helps-
Rehman.

Create, Add, Delete content type (attach a custom document template)

July 9, 2012 2 comments

Creates a custom content type…attaches a custom document template with it…enables content types on document library…..deletes “Document” content type from the library….attaches our custom content type to it…

private void ProcessMyCVContentType(SPWeb web)
        {
            try
            {
                web.AllowUnsafeUpdates = true;
                SPDocumentLibrary cvDocLib = web.Lists.TryGetList(Config.ListName_MyCVs) as SPDocumentLibrary;
                SPContentTypeId myContentTypeId = new SPContentTypeId("0x010100FA0963FA69A648AA916D2E41284FC3D5");
                SPContentType myContentType = web.ContentTypes[myContentTypeId];
                if (myContentType == null)
                {
                    myContentType = new SPContentType(myContentTypeId, web.ContentTypes, "MyCVs");
                    web.ContentTypes.Add(myContentType);
                    myContentType.Group = "Custom Content Types";
                    myContentType.DocumentTemplate = "/my/_cts/myconenttypetemplate/template.dotx"; //SharedUrlUtility.EnsureTrailingSlash(web.ServerRelativeUrl) + Config.CVTemplatePath; //must be a relative url, no http:// etc in it.....otherwise u get "value does not fall within the expected range"......... 
SPFieldLink fieldLink = new SPFieldLink(cvDocLib.Fields["Primary"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    fieldLink = new SPFieldLink(cvDocLib.Fields["Description"]);
                    myContentType.FieldLinks.Add(fieldLink);
                    myContentType.Update();
                }                            
                                            
                if (myContentType != null && cvDocLib != null)
                {
                    cvDocLib.ContentTypesEnabled = true;
                    if (cvDocLib.ContentTypes["Document"] != null)
                        cvDocLib.ContentTypes.Delete(cvDocLib.ContentTypes["Document"].Id);
                    cvDocLib.ContentTypes.Add(myContentType);
                    cvDocLib.Update();
                }                       
            }
            catch (Exception ex)
            {
                Log.LogError("MyProjName", ex);
            }
        }

make sure to put your custom template (e.g. customtemplate.dotx) at the specified location or a location of your choice (usually _cts folder is used for content types, see in sharepoint designer) and mention a relative url to it…….

Hope this helps-
Rehman.

Enable Full Toolbar on an XSLT List View Web Part (XsltListViewWebPart) / on a list

July 9, 2012 3 comments

Enabling toolbar for a list programmatically means to enable it on xslt list view web part..Here’s the code:

private void EnableToolbarOnXsltListViewWP(string viewName, string listName, string webUrl)
        {
            using (var spSite = new SPSite(webUrl))
            {
                using (SPWeb web = spSite.OpenWeb())
                {
                    spSite.AllowUnsafeUpdates = true;
                    web.AllowUnsafeUpdates = true;
                    SPList spList = web.Lists[listName];
                    SPView spView = spList.Views[viewName];
                    SPLimitedWebPartManager spLimitedWebPartManager = web.GetLimitedWebPartManager(spView.Url, PersonalizationScope.Shared);
                    foreach (System.Web.UI.WebControls.WebParts.WebPart webPart in spLimitedWebPartManager.WebParts)
                    {
                        if (webPart.GetType().Name.Equals("XsltListViewWebPart") && webPart.Title == Config.ListName_MyCVs)
                        {
                            try
                            {
                                MethodInfo ensureViewMethod = webPart.GetType().GetMethod("EnsureView", BindingFlags.Instance | BindingFlags.NonPublic);
                                object[] ensureViewParams = { };
                                ensureViewMethod.Invoke(webPart, ensureViewParams);
                                FieldInfo viewFieldInfo = webPart.GetType().GetField("view", BindingFlags.NonPublic | BindingFlags.Instance);
                                var view = viewFieldInfo.GetValue(webPart) as SPView;
                                Type[] toolbarMethodParamTypes = { Type.GetType("System.String") };
                                MethodInfo setToolbarTypeMethod = view.GetType().GetMethod("SetToolbarType", BindingFlags.Instance | BindingFlags.NonPublic, null, toolbarMethodParamTypes, null);
                                object[] setToolbarParam = { "ShowToolbar" };
                                setToolbarTypeMethod.Invoke(view, setToolbarParam);
                                view.Update();
                            }
                            catch (Exception ex)
                            {
                                Log.LogError("MyProjName", ex);
                            }
                        }
                    }
                    spSite.AllowUnsafeUpdates = false;
                }
            }
        }

Hope this helps-
Rehman.

Hide Site Actions


<!--Hide Site Actions from readers/visitors -->

<Sharepoint:SPSecurityTrimmedControl runat="server" Permissions="EditListItems">

<!-- Site Actions content here... -->

</Sharepoint:SPSecurityTrimmedControl>

Hope this helps-
Rehman.