Friday, 28 February 2014

VS 12 Issue – Cannot add SharePoint Project Items to Feature. Object reference not set to an instance of an object.


Hi Again,

Today new issue J very small finding but thought to share with all
Problem:
We have SharePoint 2013 and Visual Studio 2012 and I have one feature and I am trying to add module to feature but I couldn’t add it getting following exception



Fig 1 : Exception

 
Googled a bit but no luck L
Solution:
 
Only solution I have found for this issue is to restart the Visual Studio. I restarted the Visual Studio and then could successfully add the module to Feature J if you have faced such issue then please share an experience.
Thanks!

Enjoy reading J
Feel free to comment / feedback if any or if you have any query J

Thursday, 27 February 2014

Duplicate Web Parts issue while creating new page


Hi again,
Today something regarding one of the new feature of SharePoint 13.  There is new attribute “ReplaceContent=TRUE” to the <File> element in <Module>.  

Till the SharePoint 2010, whenever we reactivate PageLayout feature (feature which provisions the page layouts in master page gallery), web parts which are added on page layout get duplicated on newly created page and we do not have very standard solution to resolve this problem except writing feature receiver and remove duplicated web parts.

In SharePoint 2013 new attribute “ReplaceContent=TRUE” is there to <File> element, and if we used this while provisioning page layout then it overrides the existing web parts on the page layouts. So whenever we create new page from the given page layouts, web parts are not get duplicated.

Example:

<Elements xmlns="http://schemas.microsoft.com/sharepoint/">

  <Module Name="MyPageLayouts" Url="_catalogs/masterpage" RootWebOnly="TRUE">

    <File Path="MyPageLayouts\MyPageLayout.aspx" Url="MyPageLayouts.Intranet/MyPageLayout.aspx" Type="GhostableInLibrary" ReplaceContent="TRUE">

<!—- All our web parts goes here -->

    </File>

  </Module>

Have a look in above example, I have set the ReplaceContent=”TRUE”. Because of this whenever I have change or add/delete web part in this elements.xml file and I need to reactivate the feature, web parts on page layouts are not get duplicated but they are get overridden.

Other Usage example:

I’ll like to share one more scenario related to this, I have sandbox solution and which I am deploying on Office 365. I was uploading my package file (.wsp) in Solution Gallery and activating it. In my package I have one feature which deploying my css file in master page gallery since I do not have file system access. I had a change in css file, I did the change, packaged the solution, deactivated the solution first, uploaded in solution gallery and activated it. But my css changes are not reflected. Then I had thought of “ReplaceContent” attribute and I have used it in module element while provisioning the css file and wonder happen, it worked like a charm.

Feel free if you have some thoughts on this or please share your experience.

Thanks!
Enjoy reading J

Feel free to comment / feedback if any or if you have any query J

Monday, 24 February 2014

The URL ' ' is invalid. It may refer to a non existent file or folder, or refer to a valid file or folder that is not in the current Web


Hi All,

Today faced very weird issue and couldn’t understand what exact problem is. So I thought I must share this who knows you may already have this kind of problem and you solved it J
I have one custom CBS (Content by Search) web part and which I am deploying through the feature. I have module which uploads the web part in web part gallery. First time it worked well. My custom CBS web part is provisioned well in web part gallery and I could add it on page successfully. But by mistake web part is deleted from gallery by some user. So customer reported bug that they couldn’t provision the web part on the page.

I thought I would re-activate the feature and web part would be again in gallery. So I reactivated the feature but no luck. After reactivating also I couldn’t find web part in web part gallery. Then I am trying to manually upload the web part in web part gallery. When I am trying to manually upload the web part in web part gallery I am getting a following exception in ULS logs

0x2B48        SharePoint Foundation                 Runtime                               tkau        Unexpected        System.Runtime.InteropServices.COMException: The URL '_catalogs/wp/MyCustomWebPart.webpart' is invalid.  It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web. [Managed code transition]    at Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object punkFile, Int64 cbFile, Object punkSPFileMgr, Object punkFFM, Int64 PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, String bstrLockIdMatch, String bstEtagToMatch, Int32 lockType, String lockId, Int32 minutes, Int32 fRefreshLock, Int32 bValidateReqFields, Guid gNewDocId, SPFi...        b857779c-f5cc-6094-c775-f22d93305018

02/24/2014 11:51:47.47*        w3wp.exe (0x1A7C)                               0x2B48        SharePoint Foundation                 Runtime                               tkau        Unexpected        ...leSaveParams sfsp, SPFileInfo& pFileProps, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& pEtagReturn, Byte& piLevel, Int32& pbIgnoredReqProps)     at Microsoft.SharePoint.Library.SPRequest.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object punkFile, Int64 cbFile, Object punkSPFileMgr, Object punkFFM, Int64 PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, String bstrLockIdMatch, String bstEtagToMatch, Int32 lockType, String lockId, Int32 minutes, Int32 fRefreshLock, Int32 bValidateReqFields, Guid gNewDocId, SPFileSaveParams sfsp, SPFileInfo& pFileProps, UInt32& pdwVirusCheckStatus, String& pVirusCheckMessage, String& p...        b857779c-f5cc-6094-c775-f22d93305018

Application error when access /_layouts/15/Upload.aspx, Error=<nativehr>0x81020030</nativehr><nativestack></nativestack>The URL '_catalogs/wp/MyCustomWebPart.webpart' is invalid.  It may refer to a nonexistent file or folder, or refer to a valid file or folder that is not in the current Web.   at Microsoft.SharePoint.Library.SPRequestInternalClass.PutFile(String bstrUrl, String bstrWebRelativeUrl, Object punkFile, Int64 cbFile, Object punkSPFileMgr, Object punkFFM, Int64 PutFileOpt, String bstrCreatedBy, String bstrModifiedBy, Int32 iCreatedByID, Int32 iModifiedByID, Object varTimeCreated, Object varTimeLastModified, Object varProperties, String bstrCheckinComment, String bstrLockIdMatch, String bstEtagToMatch, Int32 lockType, String lockId, Int32 minutes, Int32 fRefreshLock...        b857779c-f5cc-6094-c775-f22d93305018

 
Fig 1 : Exception
 

I did googled at least for couple of hours for the above exception but no luck. It seems that file is already available in web part gallery but couldn’t visible so I tried several following options:
  1. Verified in SharePoint manager but no such existing file can be found.
  2. Then I accessed web part gallery using powershell but also no such file exist.
  3. I deleted the entries from Recycle Bin but still no luck
Then I renamed the file and uploaded it and wonder happen, it asked me property edit box so I again renamed the name to original name and then different following error:
 


Fig 2: Exception
 
So here my guess was correct that file is in web part gallery but couldn’t see it.
After trying couple of hours for this exception and no luck then finally I renamed the file and uploaded it successfully. This is workaround I need to do and still not understand what exact problem is.
Feel free if you have some thoughts on this or please share your experience in case you have faced similar kind of error and solved.
Thanks!
Enjoy reading J
 
Feel free to comment / feedback if any or if you have any query J 
 
 

Sunday, 23 February 2014

JSOM: Accessing Site Collection / Web properties and user profile properties

Hi Again,
This time some findings related to JavaScript Object Model which I learn so I thought I should share this with all. May be already shared by another techie.  
 
I’ll keep updating this blog as I’ll find new stuff related to JSOM.
 
1.   We have a requirement like to read the site collection properties in Java Script.  First of all there is no property bag for SiteCollection. Those properties are stored in root web’s property bag.
Following is the sample code for accessing web property in JavaScript. Following code will require SP.Js file to be loaded.
//Make sure that the SP.js file is loaded
SP.SOD.executeOrDelayUntilScriptLoaded(getCustomWebProperty, 'SP.js');
function getCustomWebProperty(){
var customWebProperty;
//fetch the current context
var context = SP.ClientContext.get_current();
//fetch the all properties of current web
allWebProperties = context.get_web().get_allProperties();
context.load(allWebProperties);
       context.executeQueryAsync(
        function () {
                   //get specific property
          customWebProperty                    
= allWebProperties.get_fieldValues().myCustomWebProperty;
        });
                   }
 
2.   Similarly we have also requirement to read the user profile property in JavaScript. One quick update here is we couldn’t update user profile properties from JavaScript, we can just read those.
Following is the sample code for accessing web property in JavaScript. Following code will require SP.UserProfiles.Js file to be loaded.
//Make sure that the SP.UserProfiles.js file is loaded
SP.SOD.executeOrDelayUntilScriptLoaded(getUserProfileProperty, 'SP.UserProfiles.js');
function getUserProfileProperty(){
var customUserProfileProperty;
var context = SP.ClientContext.get_current();
//people manager provides methods for operation related to people
var mgrPeople = new SP.UserProfiles.PeopleManager(context);
//fetch the user properties of current user – Return Type: SP.UserProfiles.PersonProperties
var myProps = mgrPeople.getMyProperties();
context.load(myProps);
context.executeQueryAsync(
function () {
                //fetch the user profile properties for the user.
                var profileProps = myProps.get_userProfileProperties();
//fetch the specific property
customUserProfileProperty =
profileProps.MyCustomUserProfilePropertyName;
        });
}
Thanks!
Enjoy reading J
Feel free to comment / feedback if any or if you have any query J

Wednesday, 12 February 2014

Identifying Template name of custom site template


Hello All,

Today, new SharePoint challenge. 

Requirement: Pro-grammatically creating the webs from custom site template.  

Challenge: Getting the template name of custom site template since OM of creating web requires the template name as

public SPWeb Add(
        string strWebUrl,
        string strTitle,
        string strDescription,
        uint nLCID,
        string strWebTemplate,
        bool useUniquePermissions,
        bool bConvertIfThere
)

Where:
strWebTemplate
A string that contains the name of the site definition configuration or site template.

Solution:

So we did our changes in OOB team site, save it as template and uploaded in solution gallery as






Fig 1. Our custom site template in Solution Gallery 

 From UI we created a site using new template and it worked fine. New site is successfully getting created with our changes from new template.

So I thought we can get WebTemplate property of newly created site and using it in our code to create the multiple sites. But this doesn’t happen as expected. WebTemplate property of new site returns value the “STS”, template of the site definition of site from where we did the “Save As Template”.

This is surprising because then we are not able to create the sites from our new site template since API for creating new web requires the template name. Really wondering how we will get the exact template name so that we could complete our requirement.

After digging into more delves, we have executed the powershell command on our site collection as follows:

$site = Get-SPSite “My Site collection URL”
$site.GetWebTemplates(1033) //local ID as parameter

The above command will enlist all the web templates available for this site collection in following format


Fig 2. List of available site templates for the given web for locale 1033

Then I could see that our template name is shown something like “GUID#MyWSPName” then to see the complete GUID I have executed following command and it looks like as follows:


Fig 3. PowerShell command to get the only web templates name

And result is

Fig 4. web templates name – Name of my custom web template

I really wonder from where this GUID came, and then I downloaded the WSP, renamed it to .CAB and extracted it. Folder structure looks like as follows:
  
Fig 5. Folder structure of extracting the my custom web template WSP

There is folder for “WebTemplate” which contains the Onet.xml file and WebTemplate file. Basically site template is deployed as WebTemplate.  I have opened the feature.xml file and feature id is same as in template name.

<Feature Id="{b94442b8-e30c-4204-b085-57d12d781aad}" Title="Web Template feature of exported web template "  Version="1.0.0.0" Scope="Site" Hidden="FALSE" RequireResources="TRUE" xmlns="http://schemas.microsoft.com/sharepoint/">
                <ElementManifests>
                                <ElementManifest Location="Elements.xml" />
                                <ElementFile Location="resources\resources.fi-FI.resx" />
                                <ElementFile Location="MyCustomSitetemplate\ONet.xml" />
                </ElementManifests>
                <Properties>
                                <Property Key="GeneratedBySaveAsTemplate" Value="1" />
                </Properties>
</Feature>

So I got the answer, basically it is the id of the feature which deploys the custom site template as a WebTemplate. Then finally I tried by creating new web with this new template name and it created successfully.

Thanks!

Enjoy reading J

Feel free to comment / feedback if any or if you have any query J